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

108 lines
3.7 KiB
C#
Raw Normal View History

2024-01-10 14:18:59 +00:00
using BotSharp.Plugin.WebDriver.Services;
2024-01-23 23:14:57 +00:00
using System.Threading;
2024-01-10 14:18:59 +00:00
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
{
public async Task ChangeListValue(Agent agent, BrowsingContextIn context, string messageId)
{
// Retrieve the page raw html and infer the element path
var body = await _instance.Page.QuerySelectorAsync("body");
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-01-27 15:20:02 +00:00
var htmlElementContextOut = await driverService.LocateElement(agent,
string.Join("", str),
context.ElementName,
messageId);
2024-01-27 14:58:51 +00:00
ILocator element = Locator(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)
var control = await _instance.Page.QuerySelectorAsync($"#{htmlElementContextOut.ElementId}");
// Show the element by modifying its CSS styles
await _instance.Page.EvaluateAsync(@"(element) => {
element.style.display = 'block';
element.style.visibility = 'visible';
}", control);
}
await element.FocusAsync();
await element.SelectOptionAsync(new SelectOptionValue
{
Label = context.UpdateValue
});
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)
var control = await _instance.Page.QuerySelectorAsync($"#{htmlElementContextOut.ElementId}");
// Show the element by modifying its CSS styles
await _instance.Page.EvaluateAsync(@"(element) => {
element.style.display = 'none';
element.style.visibility = 'hidden';
}", control);
}
2024-01-10 14:18:59 +00:00
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}