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

141 lines
4.5 KiB
C#
Raw Normal View History

2024-05-17 23:45:18 +00:00
using System.Xml.Linq;
2024-03-08 13:17:36 +00:00
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
{
2024-04-08 15:09:08 +00:00
/// <summary>
/// Using attributes or text to locate element and return the selector
/// </summary>
/// <param name="message"></param>
/// <param name="location"></param>
/// <returns></returns>
2024-03-08 13:17:36 +00:00
public async Task<BrowserActionResult> LocateElement(MessageInfo message, ElementLocatingArgs location)
{
var result = new BrowserActionResult();
2024-04-09 03:38:56 +00:00
var page = _instance.GetPage(message.ContextId);
2024-03-08 13:17:36 +00:00
ILocator locator = page.Locator("body");
int count = 0;
// check if selector is specified
if (location.Selector != null)
{
2024-05-17 23:45:18 +00:00
locator = locator.Locator(location.Selector);
2024-03-08 13:17:36 +00:00
count = await locator.CountAsync();
}
2024-05-23 12:05:36 +00:00
if (location.Tag != null)
{
locator = page.Locator(location.Tag);
count = await locator.CountAsync();
}
2024-03-08 13:17:36 +00:00
// try attribute
2024-05-17 23:45:18 +00:00
if (!string.IsNullOrEmpty(location.AttributeName))
2024-03-08 13:17:36 +00:00
{
locator = locator.Locator($"[{location.AttributeName}='{location.AttributeValue}']");
count = await locator.CountAsync();
}
// Retrieve the page raw html and infer the element path
if (!string.IsNullOrEmpty(location.Text))
{
var regexExpression = location.MatchRule.ToLower() switch
{
"startwith" => $"^{location.Text}",
"endwith" => $"{location.Text}$",
"contains" => $"{location.Text}",
_ => $"^{location.Text}$"
};
var regex = new Regex(regexExpression, RegexOptions.IgnoreCase);
locator = locator.GetByText(regex);
count = await locator.CountAsync();
// try placeholder
if (count == 0)
{
locator = locator.GetByPlaceholder(regex);
count = await locator.CountAsync();
}
}
2024-03-22 19:12:45 +00:00
if (location.Index >= 0)
2024-03-08 13:17:36 +00:00
{
locator = locator.Nth(location.Index);
count = await locator.CountAsync();
}
if (count == 0)
{
2024-04-08 15:09:08 +00:00
result.Message = $"Can't locate element by keyword {location.Text}";
_logger.LogError(result.Message);
2024-03-08 13:17:36 +00:00
}
else if (count == 1)
{
2024-05-23 12:05:36 +00:00
if (location.Parent)
{
locator = locator.Locator("..");
}
2024-03-08 13:17:36 +00:00
result.Selector = locator.ToString().Split('@').Last();
2024-05-17 23:45:18 +00:00
// Make sure the element is visible
2024-05-23 12:05:36 +00:00
/*if (!await locator.IsVisibleAsync())
{
await locator.EvaluateAsync("element => element.style.height = '15px'");
await locator.EvaluateAsync("element => element.style.width = '15px'");
await locator.EvaluateAsync("element => element.style.opacity = '1.0'");
}*/
2024-05-17 23:45:18 +00:00
var html = await locator.InnerHTMLAsync();
result.Body = html;
2024-03-08 13:17:36 +00:00
result.IsSuccess = true;
}
else if (count > 1)
{
2024-05-17 23:45:18 +00:00
// Make sure the element is visible
foreach (var element in await locator.AllAsync())
{
if (!await element.IsVisibleAsync())
{
await element.EvaluateAsync("element => element.style.height = '10px'");
await element.EvaluateAsync("element => element.style.width = '10px'");
await element.EvaluateAsync("element => element.style.opacity = '1.0'");
}
}
2024-03-08 13:17:36 +00:00
if (location.FailIfMultiple)
{
2024-04-08 15:09:08 +00:00
result.Message = $"Multiple elements are found by {locator}";
_logger.LogError(result.Message);
2024-03-08 13:17:36 +00:00
foreach (var element in await locator.AllAsync())
{
var content = await element.InnerHTMLAsync();
_logger.LogError(content);
}
}
else
{
result.Selector = locator.ToString();
result.IsSuccess = true;
}
}
2024-04-08 15:09:08 +00:00
// Hightlight the element
if (result.IsSuccess && location.Highlight)
{
var handle = await page.QuerySelectorAsync(result.Selector);
await page.EvaluateAsync($@"
(element) => {{
element.style.outline = '2px solid red';
}}", handle);
result.IsHighlighted = true;
}
2024-03-08 13:17:36 +00:00
return result;
}
}