剧作家自动化异常 - Page.LocatorOptions().setHasText() 给出 2 个元素。如何唯一识别

问题描述 投票:0回答:2

我正在尝试使用 Playwright-Java 从 Angular 下拉列表中选择一个值。由于发现多个值而失败。如何通过文字唯一识别。

    getPage().locator(obj).click();

    Locator list = getPage().locator("//span[@class='mat-option-text']", new Page.LocatorOptions().setHasText(value));

    list.click();

异常: com.microsoft.playwright.PlaywrightException:错误{ message='错误:严格模式违规:locator("//span[@class='mat-option-text']").filter(new Locator.FilterOptions().setHasText("Corporation")) 解析为 2 个元素:

playwright playwright-test playwright-typescript playwright-java
2个回答
0
投票

如果你想使用 CSS 选择器 - 你可以使用

first()

   page.locator(".mat-option-text", { hasText: "Corporation" }).first()

另一个选项 getByText 或 getByRole -

exact
可以提供帮助

   page.getByRole("option", { name: "Corporation", exact: true })
   // or
   page.getByText("Corporation", { exact: true })

或者用Java

   page.getByText("Corporation", new Page.GetByTextOptions().setExact(true)

0
投票

我能够通过循环遍历所有元素列表并对预期元素设置条件来解决我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.