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

102 lines
3.3 KiB
C#
Raw Normal View History

2024-01-03 19:40:41 +00:00
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
{
2024-03-08 13:17:36 +00:00
public async Task<BrowserActionResult> ClickButton(BrowserActionParams actionParams)
2024-01-03 19:40:41 +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-06 05:05:52 +00:00
2024-02-02 04:16:57 +00:00
// Find by text exactly match
2024-04-09 03:38:56 +00:00
var elements = _instance.GetPage(actionParams.ContextId)
.GetByRole(AriaRole.Button, new PageGetByRoleOptions
{
Name = actionParams.Context.ElementName
});
2024-02-06 05:05:52 +00:00
var count = await elements.CountAsync();
if (count == 0)
{
2024-04-09 03:38:56 +00:00
elements = _instance.GetPage(actionParams.ContextId)
.GetByRole(AriaRole.Link, new PageGetByRoleOptions
{
Name = actionParams.Context.ElementName
});
2024-02-06 05:05:52 +00:00
count = await elements.CountAsync();
}
2024-02-02 04:16:57 +00:00
2024-02-08 21:39:56 +00:00
if (count == 0)
{
2024-04-09 03:38:56 +00:00
elements = _instance.GetPage(actionParams.ContextId)
.GetByText(actionParams.Context.ElementName);
2024-02-08 21:39:56 +00:00
count = await elements.CountAsync();
}
2024-02-06 05:05:52 +00:00
if (count == 0)
2024-02-02 04:16:57 +00:00
{
// Infer element if not found
var driverService = _services.GetRequiredService<WebDriverService>();
2024-04-09 03:38:56 +00:00
var html = await FilteredButtonHtml(actionParams.ContextId);
2024-02-06 05:05:52 +00:00
var htmlElementContextOut = await driverService.InferElement(actionParams.Agent,
2024-02-02 04:16:57 +00:00
html,
2024-02-06 05:05:52 +00:00
actionParams.Context.ElementName,
actionParams.MessageId);
2024-04-09 03:38:56 +00:00
elements = Locator(actionParams.ContextId, htmlElementContextOut);
2024-02-06 05:05:52 +00:00
if (elements == null)
{
2024-03-08 13:17:36 +00:00
var errorMessage = $"Can't locate element by keyword {actionParams.Context.ElementName}";
2024-04-08 15:09:08 +00:00
result.Message = errorMessage;
2024-03-08 13:17:36 +00:00
return result;
2024-02-06 05:05:52 +00:00
}
2024-02-02 04:16:57 +00:00
}
2024-02-06 05:05:52 +00:00
try
{
await elements.ClickAsync();
2024-04-09 03:38:56 +00:00
await _instance.Wait(actionParams.ContextId);
2024-03-08 13:17:36 +00:00
result.IsSuccess = true;
2024-02-06 05:05:52 +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-03-08 13:17:36 +00:00
return result;
2024-02-02 04:16:57 +00:00
}
2024-04-09 03:38:56 +00:00
private async Task<string> FilteredButtonHtml(string contextId)
2024-02-02 04:16:57 +00:00
{
2024-01-27 15:20:02 +00:00
var driverService = _services.GetRequiredService<WebDriverService>();
2024-01-03 19:40:41 +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(contextId).QuerySelectorAsync("body");
2024-01-03 19:40:41 +00:00
var str = new List<string>();
2024-01-06 03:24:13 +00:00
/*var anchors = await body.QuerySelectorAllAsync("a");
2024-01-03 19:40:41 +00:00
foreach (var a in anchors)
{
var text = await a.TextContentAsync();
2024-01-03 21:10:53 +00:00
str.Add($"<a>{(string.IsNullOrEmpty(text) ? "EMPTY" : text)}</a>");
2024-01-06 03:24:13 +00:00
}*/
2024-01-03 19:40:41 +00:00
var buttons = await body.QuerySelectorAllAsync("button");
foreach (var btn in buttons)
{
var text = await btn.TextContentAsync();
2024-01-06 03:24:13 +00:00
var name = await btn.GetAttributeAsync("name");
var id = await btn.GetAttributeAsync("id");
2024-01-27 15:20:02 +00:00
str.Add(driverService.AssembleMarkup("button", new MarkupProperties
{
Id = id,
Name = name,
Text = text
}));
2024-01-03 19:40:41 +00:00
}
2024-02-02 04:16:57 +00:00
return string.Join("", str);
2024-01-03 19:40:41 +00:00
}
}