BotSharp/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.InputUserPassword.cs

38 lines
1.2 KiB
C#
Raw Normal View History

2024-02-06 05:05:52 +00:00
using Microsoft.Extensions.Logging;
2024-01-06 03:24:13 +00:00
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
{
2024-02-06 05:05:52 +00:00
public async Task<bool> InputUserPassword(BrowserActionParams actionParams)
2024-01-06 03:24:13 +00:00
{
await _instance.Wait(actionParams.ConversationId);
2024-02-01 03:29:13 +00:00
2024-01-06 03:24:13 +00:00
// Retrieve the page raw html and infer the element path
var body = await _instance.GetPage(actionParams.ConversationId)
.QuerySelectorAsync("body");
2024-01-06 03:24:13 +00:00
var inputs = await body.QuerySelectorAllAsync("input");
var password = inputs.FirstOrDefault(x => x.GetAttributeAsync("type").Result == "password");
if (password == null)
{
2024-02-06 05:05:52 +00:00
throw new Exception($"Can't locate the web element {actionParams.Context.ElementName}.");
2024-01-06 03:24:13 +00:00
}
var config = _services.GetRequiredService<IConfiguration>();
try
{
2024-02-06 05:05:52 +00:00
var key = actionParams.Context.Password.Replace("@", "").Replace(".", ":");
2024-01-06 03:24:13 +00:00
var value = config.GetValue<string>(key);
await password.FillAsync(value);
2024-02-06 05:05:52 +00:00
return true;
2024-01-06 03:24:13 +00:00
}
catch (Exception ex)
{
2024-02-06 05:05:52 +00:00
_logger.LogError(ex.Message);
2024-01-06 03:24:13 +00:00
}
2024-02-06 05:05:52 +00:00
return false;
2024-01-06 03:24:13 +00:00
}
}