2024-02-02 04:16:57 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
|
|
|
|
|
|
|
|
|
public partial class PlaywrightWebDriver
|
|
|
|
|
{
|
|
|
|
|
public async Task ClickElement(Agent agent, BrowsingContextIn context, string messageId)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// Retrieve the page raw html and infer the element path
|
2024-02-04 22:55:46 +00:00
|
|
|
var regexExpression = context.MatchRule.ToLower() switch
|
|
|
|
|
{
|
|
|
|
|
"startwith" => $"^{context.ElementText}",
|
|
|
|
|
"endwith" => $"{context.ElementText}$",
|
|
|
|
|
"contains" => $"{context.ElementText}",
|
|
|
|
|
_ => $"^{context.ElementText}$"
|
|
|
|
|
};
|
|
|
|
|
var regex = new Regex(regexExpression, RegexOptions.IgnoreCase);
|
2024-02-02 04:16:57 +00:00
|
|
|
var elements = _instance.Page.GetByText(regex);
|
|
|
|
|
var count = await elements.CountAsync();
|
|
|
|
|
|
|
|
|
|
// try placeholder
|
|
|
|
|
if (count == 0)
|
|
|
|
|
{
|
|
|
|
|
elements = _instance.Page.GetByPlaceholder(regex);
|
|
|
|
|
count = await elements.CountAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
|
{
|
2024-02-04 22:55:46 +00:00
|
|
|
throw new Exception($"Can't locate element by keyword {context.ElementText}");
|
2024-02-02 04:16:57 +00:00
|
|
|
}
|
|
|
|
|
else if (count > 1)
|
|
|
|
|
{
|
2024-02-04 22:55:46 +00:00
|
|
|
_logger.LogWarning($"Multiple elements are found by keyword {context.ElementText}");
|
2024-02-02 04:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await elements.ClickAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|