From 70b3e96c0aeba9c874b56748a51951e47a514581 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Mon, 15 Apr 2024 23:48:22 -0500 Subject: [PATCH] Click element by position. --- .../Browsing/Models/ElementActionArgs.cs | 10 +++++-- .../Browsing/Models/ElementPosition.cs | 8 +++++ .../BotSharp.Core/BotSharp.Core.csproj | 4 +-- .../BotSharp.Plugin.SqlDriver.csproj | 3 +- .../PlaywrightDriver/PlaywrightInstance.cs | 10 +++---- .../PlaywrightWebDriver.DoAction.cs | 16 +++++++++- .../PlaywrightWebDriver.LaunchBrowser.cs | 29 +++++++++---------- 7 files changed, 51 insertions(+), 29 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementPosition.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementActionArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementActionArgs.cs index dcda7436..48eb6a26 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementActionArgs.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementActionArgs.cs @@ -7,12 +7,16 @@ public class ElementActionArgs private BroswerActionEnum _action; public BroswerActionEnum Action => _action; - private string _content; - public string Content => _content; + private string? _content; + public string? Content => _content; - public ElementActionArgs(BroswerActionEnum action) + private ElementPosition? _position; + public ElementPosition? Position => _position; + + public ElementActionArgs(BroswerActionEnum action, ElementPosition? position = null) { _action = action; + _position = position; } public ElementActionArgs(BroswerActionEnum action, string content) diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementPosition.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementPosition.cs new file mode 100644 index 00000000..fd95bd66 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementPosition.cs @@ -0,0 +1,8 @@ +namespace BotSharp.Abstraction.Browsing.Models; + +public class ElementPosition +{ + public float X { get; set; } = default!; + + public float Y { get; set; } = default!; +} diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index ece7daa7..4b7bfff6 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -133,8 +133,8 @@ - - + + diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj b/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj index 171ba4cd..383a9f4e 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 @@ -33,7 +33,6 @@ - diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs index 9786ab93..a801400b 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -40,13 +40,13 @@ public class PlaywrightInstance : IDisposable Channel = "chrome", IgnoreDefaultArgs = new[] { - "--disable-infobars" - }, + "--disable-infobars" + }, Args = new[] { - "--disable-infobars", - // "--start-maximized" - } + "--disable-infobars", + // "--start-maximized" + } }); _contexts[id].Page += async (sender, e) => diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs index 93f714d2..dd4600c9 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs @@ -9,7 +9,21 @@ public partial class PlaywrightWebDriver if (action.Action == BroswerActionEnum.Click) { - await locator.ClickAsync(); + if (action.Position == null) + { + await locator.ClickAsync(); + } + else + { + await locator.ClickAsync(new LocatorClickOptions + { + Position = new Position + { + X = action.Position.X, + Y = action.Position.Y + } + }); + } } else if (action.Action == BroswerActionEnum.InputText) { diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LaunchBrowser.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LaunchBrowser.cs index e415df8c..b3afd940 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LaunchBrowser.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LaunchBrowser.cs @@ -23,24 +23,21 @@ public partial class PlaywrightWebDriver } var page = await _instance.NewPage(contextId); - - if (!string.IsNullOrEmpty(url)) + + try { - try + var response = await page.GotoAsync(url, new PageGotoOptions { - var response = await page.GotoAsync(url, new PageGotoOptions - { - Timeout = 15 * 1000 - }); - await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded); - result.IsSuccess = response.Status == 200; - } - catch(Exception ex) - { - result.Message = ex.Message; - result.StackTrace = ex.StackTrace; - _logger.LogError(ex.Message); - } + Timeout = 15 * 1000 + }); + await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded); + result.IsSuccess = response.Status == 200; + } + catch (Exception ex) + { + result.Message = ex.Message; + result.StackTrace = ex.StackTrace; + _logger.LogError(ex.Message); } }