Strict mode violation Error happens when I try select an option which is a substring of another option

huangapple go评论92阅读模式
英文:

Strict mode violation Error happens when I try select an option which is a substring of another option

问题

首先,我想告诉你我对Playwright工具还很新手。我的问题是,当我尝试使用以下代码单击列表中的male选项时,我有两个选项,malefemale

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.
}

这是因为malefemale的子字符串。如果我尝试单击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(&quot;Male&quot;)).click();

it throws the following error

Exception in thread &quot;main&quot; com.microsoft.playwright.PlaywrightException: Error {
  message=&#39;Error: strict mode violation: getByRole(AriaRole.LISTITEM).filter(new Locator.FilterOptions().setHasText(&quot;Male&quot;)) resolved to 2 elements:
    1) &lt;li id=&quot;cP1Q100&quot; class=&quot;z-comboitem&quot;&gt;…&lt;/li&gt; aka getByRole(AriaRole.LISTITEM).filter(new Locator.FilterOptions().setHasText(&quot;Female&quot;))
    2) &lt;li id=&quot;cP1Q200&quot; class=&quot;z-comboitem z-comboitem-sele…&gt;…&lt;/li&gt; aka getByRole(AriaRole.LISTITEM).filter(new Locator.FilterOptions().setHasText(&quot;Male&quot;))

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(&#39;//button&#39;).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(&quot;Male&quot;))).click();

https://playwright.dev/java/docs/locators#filtering-locators

huangapple
  • 本文由 发表于 2023年3月7日 12:51:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定