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

89 lines
2.7 KiB
C#
Raw Normal View History

2024-03-08 13:17:36 +00:00
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
public partial class PlaywrightWebDriver
{
public async Task DoAction(MessageInfo message, ElementActionArgs action, BrowserActionResult result)
{
2024-04-09 03:38:56 +00:00
var page = _instance.GetPage(message.ContextId);
2024-07-02 14:52:16 +00:00
if (string.IsNullOrEmpty(result.Selector))
{
Serilog.Log.Error($"Selector is not set.");
return;
}
ILocator locator = page.Locator(result.Selector);
2024-07-02 14:52:16 +00:00
var count = await locator.CountAsync();
2024-07-02 14:52:16 +00:00
if (count == 0)
{
Serilog.Log.Error($"Element not found: {result.Selector}");
return;
}
else if (count > 1)
{
if(!action.FirstIfMultipleFound)
{
Serilog.Log.Error($"Multiple eElements were found: {result.Selector}");
return;
}
else
{
locator = page.Locator(result.Selector).First;// 匹配到多个时取第一个否则当await locator.ClickAsync();匹配到多个就会抛异常。
}
}
2024-03-08 13:17:36 +00:00
2024-04-08 15:09:08 +00:00
if (action.Action == BroswerActionEnum.Click)
2024-03-08 13:17:36 +00:00
{
2024-04-16 04:48:22 +00:00
if (action.Position == null)
{
await locator.ClickAsync();
}
else
{
await locator.ClickAsync(new LocatorClickOptions
{
Position = new Position
{
X = action.Position.X,
Y = action.Position.Y
}
});
}
2024-03-08 13:17:36 +00:00
}
2024-04-08 15:09:08 +00:00
else if (action.Action == BroswerActionEnum.InputText)
{
await locator.FillAsync(action.Content);
2024-05-17 23:45:18 +00:00
if (action.PressKey != null)
{
2024-11-18 18:52:59 +00:00
if (action.DelayBeforePressingKey > 0)
{
await Task.Delay(action.DelayBeforePressingKey);
}
2024-05-17 23:45:18 +00:00
await locator.PressAsync(action.PressKey);
}
2024-04-08 15:09:08 +00:00
}
2024-06-02 02:15:47 +00:00
else if (action.Action == BroswerActionEnum.Typing)
{
await locator.PressSequentiallyAsync(action.Content);
if (action.PressKey != null)
{
2024-11-18 18:52:59 +00:00
if (action.DelayBeforePressingKey > 0)
{
await Task.Delay(action.DelayBeforePressingKey);
}
2024-06-02 02:15:47 +00:00
await locator.PressAsync(action.PressKey);
}
}
2024-06-10 15:51:14 +00:00
else if (action.Action == BroswerActionEnum.Hover)
{
await locator.HoverAsync();
}
2024-08-01 18:18:34 +00:00
if (action.WaitTime > 0)
{
await Task.Delay(1000 * action.WaitTime);
}
2024-03-08 13:17:36 +00:00
}
}