From bb0be151c31fddac123f50b946defee721079f0c Mon Sep 17 00:00:00 2001 From: "nick.yi" Date: Tue, 17 Jun 2025 15:52:04 +0800 Subject: [PATCH] 1.IgnoreIfNotFound defaults to true 2.Fix the incomplete log issue --- .../Browsing/Models/ElementLocatingArgs.cs | 2 +- .../PlaywrightWebDriver.LocateElement.cs | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs index 78db9e86..0890b0c9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementLocatingArgs.cs @@ -30,7 +30,7 @@ public class ElementLocatingArgs public bool FailIfMultiple { get; set; } [JsonPropertyName("ignore_if_not_found")] - public bool IgnoreIfNotFound { get; set; } + public bool IgnoreIfNotFound { get; set; } = true; /// /// Draw outline around the element diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs index 1f3508af..994045b4 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.LocateElement.cs @@ -23,16 +23,19 @@ public partial class PlaywrightWebDriver } ILocator locator = page.Locator("body"); int count = 0; + var keyword = string.Empty; // check if selector is specified if (location.Selector != null) { + keyword = location.Selector; locator = locator.Locator(location.Selector); count = await locator.CountAsync(); } if (location.Tag != null) { + keyword = location.Tag; locator = page.Locator(location.Tag); count = await locator.CountAsync(); } @@ -40,13 +43,15 @@ public partial class PlaywrightWebDriver // try attribute if (!string.IsNullOrEmpty(location.AttributeName)) { - locator = locator.Locator($"[{location.AttributeName}='{location.AttributeValue}']"); + keyword = $"[{location.AttributeName}='{location.AttributeValue}']"; + locator = locator.Locator(keyword); count = await locator.CountAsync(); } // Retrieve the page raw html and infer the element path if (!string.IsNullOrEmpty(location.Text)) { + keyword = location.Text; var text = location.Text.Replace("(", "\\(").Replace(")", "\\)"); var regexExpression = location.MatchRule.ToLower() switch { @@ -69,6 +74,7 @@ public partial class PlaywrightWebDriver if (location.Index >= 0) { + keyword = $"Index:{location.Index}"; locator = locator.Nth(location.Index); count = await locator.CountAsync(); } @@ -77,13 +83,13 @@ public partial class PlaywrightWebDriver { if (location.IgnoreIfNotFound) { - result.Message = $"Can't locate element by keyword {location.Text} and Ignored"; - _logger.LogInformation(result.Message); + result.Message = $"Can't locate element by keyword {keyword} and Ignored"; + _logger.LogWarning(result.Message); } else { - result.Message = $"Can't locate element by keyword {location.Text}"; - _logger.LogInformation(result.Message); + result.Message = $"Can't locate element by keyword {keyword}"; + _logger.LogError(result.Message); } }