2024-02-04 22:55:46 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
2024-01-03 19:40:41 +00:00
|
|
|
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
|
|
|
|
|
|
|
|
|
public partial class PlaywrightWebDriver
|
|
|
|
|
{
|
2024-02-06 05:05:52 +00:00
|
|
|
public async Task<bool> InputUserText(BrowserActionParams actionParams)
|
2024-01-03 19:40:41 +00:00
|
|
|
{
|
2024-02-01 03:29:13 +00:00
|
|
|
await _instance.Page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
|
2024-02-04 22:55:46 +00:00
|
|
|
await _instance.Page.WaitForLoadStateAsync(LoadState.NetworkIdle);
|
2024-02-02 04:16:57 +00:00
|
|
|
|
|
|
|
|
// Find by text exactly match
|
|
|
|
|
var elements = _instance.Page.GetByRole(AriaRole.Textbox, new PageGetByRoleOptions
|
|
|
|
|
{
|
2024-02-06 05:05:52 +00:00
|
|
|
Name = actionParams.Context.ElementText
|
2024-02-02 04:16:57 +00:00
|
|
|
});
|
|
|
|
|
var count = await elements.CountAsync();
|
2024-02-05 04:22:43 +00:00
|
|
|
if (count == 0)
|
|
|
|
|
{
|
2024-02-06 05:05:52 +00:00
|
|
|
elements = _instance.Page.GetByPlaceholder(actionParams.Context.ElementText);
|
2024-02-05 04:22:43 +00:00
|
|
|
count = await elements.CountAsync();
|
|
|
|
|
}
|
2024-02-04 22:55:46 +00:00
|
|
|
|
2024-02-02 04:16:57 +00:00
|
|
|
if (count == 0)
|
|
|
|
|
{
|
|
|
|
|
var driverService = _services.GetRequiredService<WebDriverService>();
|
|
|
|
|
var html = await FilteredInputHtml();
|
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-02 04:16:57 +00:00
|
|
|
elements = Locator(htmlElementContextOut);
|
2024-02-06 05:05:52 +00:00
|
|
|
count = await elements.CountAsync();
|
2024-02-02 04:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-06 05:05:52 +00:00
|
|
|
if (count == 0)
|
2024-02-02 04:16:57 +00:00
|
|
|
{
|
2024-02-06 05:05:52 +00:00
|
|
|
|
2024-02-02 04:16:57 +00:00
|
|
|
}
|
2024-02-06 05:05:52 +00:00
|
|
|
else if (count == 1)
|
2024-02-02 04:16:57 +00:00
|
|
|
{
|
2024-02-06 05:05:52 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await elements.FillAsync(actionParams.Context.InputText);
|
|
|
|
|
if (actionParams.Context.PressEnter.HasValue && actionParams.Context.PressEnter.Value)
|
|
|
|
|
{
|
|
|
|
|
await elements.PressAsync("Enter");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Triggered ajax
|
|
|
|
|
await _instance.Page.WaitForLoadStateAsync(LoadState.NetworkIdle);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex.Message);
|
|
|
|
|
}
|
2024-02-02 04:16:57 +00:00
|
|
|
}
|
2024-02-06 05:05:52 +00:00
|
|
|
|
|
|
|
|
return false;
|
2024-02-02 04:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<string> FilteredInputHtml()
|
|
|
|
|
{
|
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.Page.QuerySelectorAsync("body");
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|