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

40 lines
1.3 KiB
C#
Raw Normal View History

2024-01-06 03:24:13 +00:00
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
{
2024-03-08 13:17:36 +00:00
public async Task<BrowserActionResult> InputUserPassword(BrowserActionParams actionParams)
2024-01-06 03:24:13 +00:00
{
2024-03-08 13:17:36 +00:00
var result = new BrowserActionResult();
2024-04-09 03:38:56 +00:00
await _instance.Wait(actionParams.ContextId);
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
2024-04-09 03:38:56 +00:00
var body = await _instance.GetPage(actionParams.ContextId)
.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-04-08 15:09:08 +00:00
result.Message = $"Can't locate the password element by '{actionParams.Context.ElementName}'";
_logger.LogError(result.Message);
2024-03-08 13:17:36 +00:00
return result;
2024-01-06 03:24:13 +00:00
}
var config = _services.GetRequiredService<IConfiguration>();
try
{
2024-02-13 00:14:01 +00:00
await password.FillAsync(actionParams.Context.Password);
2024-03-08 13:17:36 +00:00
result.IsSuccess = true;
2024-01-06 03:24:13 +00:00
}
catch (Exception ex)
{
2024-04-08 15:09:08 +00:00
result.Message = ex.Message;
2024-03-08 13:17:36 +00:00
result.StackTrace = ex.StackTrace;
2024-02-06 05:05:52 +00:00
_logger.LogError(ex.Message);
2024-01-06 03:24:13 +00:00
}
2024-03-08 13:17:36 +00:00
return result;
2024-01-06 03:24:13 +00:00
}
}