Locate web element by attribute.

This commit is contained in:
Haiping Chen 2024-02-13 18:19:44 -06:00
parent eb462f32c6
commit 54fe8bc6fe
3 changed files with 45 additions and 17 deletions

View file

@ -9,23 +9,37 @@ public partial class PlaywrightWebDriver
{ {
await _instance.Wait(actionParams.ConversationId); await _instance.Wait(actionParams.ConversationId);
// Retrieve the page raw html and infer the element path var page = _instance.GetPage(actionParams.ConversationId);
var regexExpression = actionParams.Context.MatchRule.ToLower() switch ILocator locator = default;
{ int count = 0;
"startwith" => $"^{actionParams.Context.ElementText}",
"endwith" => $"{actionParams.Context.ElementText}$",
"contains" => $"{actionParams.Context.ElementText}",
_ => $"^{actionParams.Context.ElementText}$"
};
var regex = new Regex(regexExpression, RegexOptions.IgnoreCase);
var elements = _instance.GetPage(actionParams.ConversationId).GetByText(regex);
var count = await elements.CountAsync();
// try placeholder // Retrieve the page raw html and infer the element path
if (count == 0) if (!string.IsNullOrEmpty(actionParams.Context.ElementText))
{ {
elements = _instance.GetPage(actionParams.ConversationId).GetByPlaceholder(regex); var regexExpression = actionParams.Context.MatchRule.ToLower() switch
count = await elements.CountAsync(); {
"startwith" => $"^{actionParams.Context.ElementText}",
"endwith" => $"{actionParams.Context.ElementText}$",
"contains" => $"{actionParams.Context.ElementText}",
_ => $"^{actionParams.Context.ElementText}$"
};
var regex = new Regex(regexExpression, RegexOptions.IgnoreCase);
locator = page.GetByText(regex);
count = await locator.CountAsync();
// try placeholder
if (count == 0)
{
locator = page.GetByPlaceholder(regex);
count = await locator.CountAsync();
}
}
// try attribute
if (count == 0 && !string.IsNullOrEmpty(actionParams.Context.AttributeName))
{
locator = page.Locator($"[{actionParams.Context.AttributeName}='{actionParams.Context.AttributeValue}']");
count = await locator.CountAsync();
} }
if (count == 0) if (count == 0)
@ -36,7 +50,7 @@ public partial class PlaywrightWebDriver
{ {
// var tagName = await elements.EvaluateAsync<string>("el => el.tagName"); // var tagName = await elements.EvaluateAsync<string>("el => el.tagName");
await elements.ClickAsync(); await locator.ClickAsync();
// Triggered ajax // Triggered ajax
await _instance.Wait(actionParams.ConversationId); await _instance.Wait(actionParams.ConversationId);
@ -46,7 +60,7 @@ public partial class PlaywrightWebDriver
else if (count > 1) else if (count > 1)
{ {
_logger.LogWarning($"Multiple elements are found by keyword {actionParams.Context.ElementText}"); _logger.LogWarning($"Multiple elements are found by keyword {actionParams.Context.ElementText}");
var all = await elements.AllAsync(); var all = await locator.AllAsync();
foreach (var element in all) foreach (var element in all)
{ {
var content = await element.TextContentAsync(); var content = await element.TextContentAsync();

View file

@ -19,6 +19,12 @@ public class BrowsingContextIn
[JsonPropertyName("element_text")] [JsonPropertyName("element_text")]
public string? ElementText { get; set; } public string? ElementText { get; set; }
[JsonPropertyName("attribute_name")]
public string? AttributeName { get; set; }
[JsonPropertyName("attribute_value")]
public string? AttributeValue { get; set; }
[JsonPropertyName("press_enter")] [JsonPropertyName("press_enter")]
public bool? PressEnter { get; set; } public bool? PressEnter { get; set; }

View file

@ -165,6 +165,14 @@
"type": "string", "type": "string",
"description": "text or placeholder shown in the element." "description": "text or placeholder shown in the element."
}, },
"attribute_name": {
"type": "string",
"description": "attribute name in the element"
},
"attribute_value": {
"type": "string",
"description": "attribute value in the element"
},
"match_rule": { "match_rule": {
"type": "string", "type": "string",
"description": "text matching rule: EndWith, StartWith, Contains, Match" "description": "text matching rule: EndWith, StartWith, Contains, Match"