2024-02-06 05:05:52 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
2024-01-10 14:18:59 +00:00
|
|
|
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
|
|
|
|
|
|
|
|
|
public partial class PlaywrightWebDriver
|
|
|
|
|
{
|
2024-02-06 05:05:52 +00:00
|
|
|
public async Task<bool> ChangeListValue(BrowserActionParams actionParams)
|
2024-01-10 14:18:59 +00:00
|
|
|
{
|
2024-02-12 17:11:05 +00:00
|
|
|
await _instance.Wait(actionParams.ConversationId);
|
2024-02-06 05:05:52 +00:00
|
|
|
|
2024-01-10 14:18:59 +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(actionParams.ConversationId).QuerySelectorAsync("body");
|
2024-01-10 14:18:59 +00:00
|
|
|
|
|
|
|
|
var str = new List<string>();
|
2024-01-23 23:14:57 +00:00
|
|
|
var inputs = await body.QuerySelectorAllAsync("select");
|
2024-01-10 14:18:59 +00:00
|
|
|
foreach (var input in inputs)
|
|
|
|
|
{
|
2024-01-23 23:14:57 +00:00
|
|
|
var html = "<select";
|
|
|
|
|
var id = await input.GetAttributeAsync("id");
|
|
|
|
|
if (!string.IsNullOrEmpty(id))
|
|
|
|
|
{
|
|
|
|
|
html += $" id='{id}'";
|
|
|
|
|
}
|
2024-01-10 14:18:59 +00:00
|
|
|
var name = await input.GetAttributeAsync("name");
|
2024-01-23 23:14:57 +00:00
|
|
|
if (!string.IsNullOrEmpty(name))
|
|
|
|
|
{
|
|
|
|
|
html += $" name='{id}'";
|
|
|
|
|
}
|
|
|
|
|
html += ">";
|
2024-01-10 14:18:59 +00:00
|
|
|
|
2024-01-23 23:14:57 +00:00
|
|
|
var options = await input.QuerySelectorAllAsync("option");
|
|
|
|
|
if (options != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var option in options)
|
|
|
|
|
{
|
|
|
|
|
html += "<option";
|
|
|
|
|
var value = await option.GetAttributeAsync("value");
|
|
|
|
|
if (!string.IsNullOrEmpty(value))
|
|
|
|
|
{
|
|
|
|
|
html += $" value='{value}'";
|
|
|
|
|
}
|
|
|
|
|
html += ">";
|
|
|
|
|
var text = await option.TextContentAsync();
|
|
|
|
|
if (!string.IsNullOrEmpty(text))
|
|
|
|
|
{
|
|
|
|
|
html += text;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
html += "'<NULL>'";
|
|
|
|
|
}
|
|
|
|
|
html += "</option>";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
html += "</select>";
|
|
|
|
|
str.Add(html);
|
2024-01-10 14:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var driverService = _services.GetRequiredService<WebDriverService>();
|
2024-02-06 05:05:52 +00:00
|
|
|
var htmlElementContextOut = await driverService.InferElement(actionParams.Agent,
|
|
|
|
|
string.Join("", str),
|
|
|
|
|
actionParams.Context.ElementName,
|
|
|
|
|
actionParams.MessageId);
|
2024-02-12 17:11:05 +00:00
|
|
|
ILocator element = Locator(actionParams.ConversationId, htmlElementContextOut);
|
2024-01-23 23:14:57 +00:00
|
|
|
|
2024-01-10 14:18:59 +00:00
|
|
|
try
|
|
|
|
|
{
|
2024-01-23 23:14:57 +00:00
|
|
|
var isVisible = await element.IsVisibleAsync();
|
|
|
|
|
|
|
|
|
|
if (!isVisible)
|
|
|
|
|
{
|
|
|
|
|
// Select the element you want to make visible (replace with your own selector)
|
2024-02-12 17:11:05 +00:00
|
|
|
var control = await _instance.GetPage(actionParams.ConversationId)
|
|
|
|
|
.QuerySelectorAsync($"#{htmlElementContextOut.ElementId}");
|
2024-01-23 23:14:57 +00:00
|
|
|
|
|
|
|
|
// Show the element by modifying its CSS styles
|
2024-02-12 17:11:05 +00:00
|
|
|
await _instance.GetPage(actionParams.ConversationId)
|
|
|
|
|
.EvaluateAsync(@"(element) => {
|
|
|
|
|
element.style.display = 'block';
|
|
|
|
|
element.style.visibility = 'visible';
|
|
|
|
|
}", control);
|
2024-01-23 23:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await element.FocusAsync();
|
|
|
|
|
await element.SelectOptionAsync(new SelectOptionValue
|
|
|
|
|
{
|
2024-02-06 05:05:52 +00:00
|
|
|
Label = actionParams.Context.UpdateValue
|
2024-01-23 23:14:57 +00:00
|
|
|
});
|
2024-01-26 04:42:57 +00:00
|
|
|
|
2024-01-23 23:14:57 +00:00
|
|
|
// Click on the blank area to activate posting
|
2024-01-26 04:42:57 +00:00
|
|
|
// await body.ClickAsync();
|
|
|
|
|
if (!isVisible)
|
|
|
|
|
{
|
|
|
|
|
// Select the element you want to make visible (replace with your own selector)
|
2024-02-12 17:11:05 +00:00
|
|
|
var control = await _instance.GetPage(actionParams.ConversationId)
|
|
|
|
|
.QuerySelectorAsync($"#{htmlElementContextOut.ElementId}");
|
2024-01-26 04:42:57 +00:00
|
|
|
|
|
|
|
|
// Show the element by modifying its CSS styles
|
2024-02-12 17:11:05 +00:00
|
|
|
await _instance.GetPage(actionParams.ConversationId).EvaluateAsync(@"(element) => {
|
2024-01-26 04:42:57 +00:00
|
|
|
element.style.display = 'none';
|
|
|
|
|
element.style.visibility = 'hidden';
|
|
|
|
|
}", control);
|
|
|
|
|
}
|
2024-02-06 05:05:52 +00:00
|
|
|
return true;
|
2024-01-10 14:18:59 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-02-06 05:05:52 +00:00
|
|
|
_logger.LogError(ex.Message);
|
2024-01-10 14:18:59 +00:00
|
|
|
}
|
2024-02-06 05:05:52 +00:00
|
|
|
|
|
|
|
|
return false;
|
2024-01-10 14:18:59 +00:00
|
|
|
}
|
|
|
|
|
}
|