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

121 lines
4.2 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> InputUserText(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-02 04:16:57 +00:00
2024-04-09 03:38:56 +00:00
var page = _instance.GetPage(actionParams.ContextId);
ILocator locator = default;
int count = 0;
// try attribute
if (count == 0 && !string.IsNullOrEmpty(actionParams.Context.AttributeName))
{
locator = page.Locator($"[{actionParams.Context.AttributeName}='{actionParams.Context.AttributeValue}']");
count = await locator.CountAsync();
}
2024-02-02 04:16:57 +00:00
// Find by text exactly match
if (count == 0)
{
locator = page.GetByRole(AriaRole.Textbox, new PageGetByRoleOptions
{
Name = actionParams.Context.ElementText
});
count = await locator.CountAsync();
}
2024-02-05 04:22:43 +00:00
if (count == 0)
{
locator = page.GetByPlaceholder(actionParams.Context.ElementText);
count = await locator.CountAsync();
2024-02-05 04:22:43 +00:00
}
2024-02-02 04:16:57 +00:00
if (count == 0)
{
var driverService = _services.GetRequiredService<WebDriverService>();
2024-04-09 03:38:56 +00:00
var html = await FilteredInputHtml(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.ElementText,
actionParams.MessageId);
2024-04-09 03:38:56 +00:00
locator = Locator(actionParams.ContextId, htmlElementContextOut);
count = await locator.CountAsync();
2024-02-02 04:16:57 +00:00
}
2024-03-08 13:17:36 +00:00
else if (count > 0)
2024-02-02 04:16:57 +00:00
{
2024-02-06 05:05:52 +00:00
try
{
await locator.FillAsync(actionParams.Context.InputText);
2024-02-06 05:05:52 +00:00
if (actionParams.Context.PressEnter.HasValue && actionParams.Context.PressEnter.Value)
{
await locator.PressAsync("Enter");
2024-02-06 05:05:52 +00:00
}
// Triggered ajax
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-02-02 04:16:57 +00:00
}
2024-02-06 05:05:52 +00:00
2024-03-08 13:17:36 +00:00
return result;
2024-02-02 04:16:57 +00:00
}
private async Task<string> FilteredInputHtml(string conversationId)
2024-02-02 04:16:57 +00:00
{
2024-01-27 14:58:51 +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
var body = await _instance.GetPage(conversationId).QuerySelectorAsync("body");
2024-01-03 19:40:41 +00:00
var str = new List<string>();
var inputs = await body.QuerySelectorAllAsync("input");
foreach (var input in inputs)
{
var text = await input.TextContentAsync();
2024-01-27 14:58:51 +00:00
var id = await input.GetAttributeAsync("id");
2024-01-03 19:40:41 +00:00
var name = await input.GetAttributeAsync("name");
var type = await input.GetAttributeAsync("type");
2024-02-01 03:29:13 +00:00
var placeholder = await input.GetAttributeAsync("placeholder");
2024-01-27 14:58:51 +00:00
str.Add(driverService.AssembleMarkup("input", new MarkupProperties
{
Id = id,
Name = name,
Type = type,
2024-02-01 03:29:13 +00:00
Text = text,
Placeholder = placeholder
2024-01-27 14:58:51 +00:00
}));
2024-01-03 19:40:41 +00:00
}
inputs = await body.QuerySelectorAllAsync("textarea");
foreach (var input in inputs)
{
var text = await input.TextContentAsync();
2024-01-27 14:58:51 +00:00
var id = await input.GetAttributeAsync("id");
2024-01-03 19:40:41 +00:00
var name = await input.GetAttributeAsync("name");
var type = await input.GetAttributeAsync("type");
2024-02-01 03:29:13 +00:00
var placeholder = await input.GetAttributeAsync("placeholder");
2024-01-27 14:58:51 +00:00
str.Add(driverService.AssembleMarkup("textarea", new MarkupProperties
{
Id = id,
Name = name,
Type = type,
2024-02-01 03:29:13 +00:00
Text = text,
Placeholder = placeholder
2024-01-27 14:58:51 +00:00
}));
2024-01-03 19:40:41 +00:00
}
2024-01-06 03:24:13 +00:00
2024-02-02 04:16:57 +00:00
return string.Join("", str);
2024-01-03 19:40:41 +00:00
}
}