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-02-12 17:11:05 +00:00
|
|
|
await _instance.Wait(actionParams.ConversationId);
|
2024-02-02 04:16:57 +00:00
|
|
|
|
2024-02-14 23:11:24 +00:00
|
|
|
var page = _instance.GetPage(actionParams.ConversationId);
|
|
|
|
|
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
|
2024-02-14 23:11:24 +00:00
|
|
|
if (count == 0)
|
|
|
|
|
{
|
|
|
|
|
locator = page.GetByRole(AriaRole.Textbox, new PageGetByRoleOptions
|
2024-02-12 17:11:05 +00:00
|
|
|
{
|
|
|
|
|
Name = actionParams.Context.ElementText
|
|
|
|
|
});
|
2024-02-14 23:11:24 +00:00
|
|
|
count = await locator.CountAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 04:22:43 +00:00
|
|
|
if (count == 0)
|
|
|
|
|
{
|
2024-02-14 23:11:24 +00:00
|
|
|
locator = page.GetByPlaceholder(actionParams.Context.ElementText);
|
|
|
|
|
count = await locator.CountAsync();
|
2024-02-05 04:22:43 +00:00
|
|
|
}
|
2024-02-04 22:55:46 +00:00
|
|
|
|
2024-02-02 04:16:57 +00:00
|
|
|
if (count == 0)
|
|
|
|
|
{
|
|
|
|
|
var driverService = _services.GetRequiredService<WebDriverService>();
|
2024-02-12 17:11:05 +00:00
|
|
|
var html = await FilteredInputHtml(actionParams.ConversationId);
|
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-02-14 23:11:24 +00:00
|
|
|
locator = Locator(actionParams.ConversationId, 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
|
|
|
|
|
{
|
2024-02-14 23:11:24 +00:00
|
|
|
await locator.FillAsync(actionParams.Context.InputText);
|
2024-02-06 05:05:52 +00:00
|
|
|
if (actionParams.Context.PressEnter.HasValue && actionParams.Context.PressEnter.Value)
|
|
|
|
|
{
|
2024-02-14 23:11:24 +00:00
|
|
|
await locator.PressAsync("Enter");
|
2024-02-06 05:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Triggered ajax
|
2024-02-12 17:11:05 +00:00
|
|
|
await _instance.Wait(actionParams.ConversationId);
|
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
|
|
|
}
|
|
|
|
|
|
2024-02-12 17:11:05 +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
|
2024-02-12 17:11:05 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|