Merge pull request #7 from AnonymousDotNet/lida_dev

修改Locator匹配到多个时取第一个值
This commit is contained in:
Haiping 2024-08-21 21:09:39 -05:00 committed by GitHub
commit 6bba847592
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 0 deletions

View file

@ -12,6 +12,11 @@ public class ElementActionArgs
public string? PressKey { get; set; }
/// <summary>
/// Locator option
/// </summary>
public bool FirstIfMultipleFound { get; set; } = false;
/// <summary>
/// Wait time in seconds
/// </summary>

View file

@ -13,11 +13,24 @@ public partial class PlaywrightWebDriver
ILocator locator = page.Locator(result.Selector);
var count = await locator.CountAsync();
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();匹配到多个就会抛异常。
}
}
if (action.Action == BroswerActionEnum.Click)
{