英文:
Strict mode violation Error happens when I try select an option which is a substring of another option
问题
首先,我想告诉你我对Playwright工具还很新手。我的问题是,当我尝试使用以下代码单击列表中的male
选项时,我有两个选项,male
和female
:
page.getByRole(AriaRole.LISTITEM).filter(new Locator.FilterOptions().setHasText("Male")).click();
它会引发以下错误:
Exception in thread "main" com.microsoft.playwright.PlaywrightException: Error {
message='Error: strict mode violation: getByRole(AriaRole.LISTITEM).filter(new Locator.FilterOptions().setHasText("Male")) resolved to 2 elements:
1) <li id="cP1Q100" class="z-comboitem">…</li> aka getByRole(AriaRole.LISTITEM).filter(new Locator.FilterOptions().setHasText("Female"))
2) <li id="cP1Q200" class="z-comboitem z-comboitem-sele…>…</li> aka getByRole(AriaRole.LISTITEM).filter(new Locator.FilterOptions().setHasText("Male"))
This happens because male is the substring of female. If I try to click the female option it happens without having any problem because female is unique here. How can I resolve this problem? I am using Java Playwright.
}
这是因为male
是female
的子字符串。如果我尝试单击female
选项,就不会出现问题,因为female
在这里是唯一的。我该如何解决这个问题?我正在使用Java Playwright。
英文:
First off, I want to tell you that I am new to this playwright tool. My problem is, I have two option male
and female
when I try to click male option in the list using the following code,
page.getByRole(AriaRole.LISTITEM).filter(new Locator.FilterOptions().setHasText("Male")).click();
it throws the following error
Exception in thread "main" com.microsoft.playwright.PlaywrightException: Error {
message='Error: strict mode violation: getByRole(AriaRole.LISTITEM).filter(new Locator.FilterOptions().setHasText("Male")) resolved to 2 elements:
1) <li id="cP1Q100" class="z-comboitem">…</li> aka getByRole(AriaRole.LISTITEM).filter(new Locator.FilterOptions().setHasText("Female"))
2) <li id="cP1Q200" class="z-comboitem z-comboitem-sele…>…</li> aka getByRole(AriaRole.LISTITEM).filter(new Locator.FilterOptions().setHasText("Male"))
This happens because male is the substring of female. If I try to click the female option it happens without having any problem because female is unique here. How can I resolve this problem? I am using Java Playwright.
答案1
得分: 1
我遇到了同样的问题,看起来是因为 playwright 默认搜索子字符串。要精确匹配文本,可以使用如下链接中所述的 exact
参数。
英文:
I came across this same problem and it appears that playwright searches for substrings by default. To match text exactly, use the exact
parameter as described here.
答案2
得分: 1
在JavaScript中,您使用nth(index)方法。
await page.locator('//button').nth(index).click();
英文:
In JavaScript you use nth(index) method.
await page.locator('//button').nth(index).click();
答案3
得分: 0
你可以尝试使用正则表达式(RegEx)。在Java中,我相信默认情况下是区分大小写的,而文本则不是。
page.getByRole(AriaRole.LISTITEM)
.filter(new Locator.FilterOptions().setHasText(Pattern.compile("Male"))).click();
更多信息请查看:https://playwright.dev/java/docs/locators#filtering-locators
英文:
You may wanna try to use RegEx. And Pattern in Java, I believe it's case sensitive by default, while text is not.
page.getByRole(AriaRole.LISTITEM)
.filter(new Locator.FilterOptions().setHasText(Pattern.compile("Male"))).click();
https://playwright.dev/java/docs/locators#filtering-locators
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论