【问题标题】:I'm getting an error using playwright nth-match我在使用剧作家 nth-match 时遇到错误
【发布时间】:2022-01-10 02:46:12
【问题描述】:

我刚刚发现了剧作家,我正在尝试看看它是否比硒更好用。我遇到的一个问题是,我为其编写测试脚本的应用程序中的所有项目都没有名称,这不会改变。我正在尝试使用带有文本的 nth-math 来查找“登录”的第二个实例以单击首页上的“按钮”,但我一直收到错误。

引发错误的行:

page.locator(':nth-match(:text("Login"), 2').click()

我也试过

page.click(':nth-match(:text("Login"), 2')

这两个都抛出相同的错误 playwright._impl._api_types.Error: Unexpected token "" while parsing selector ":nth-match(:text("Login"), 2"

【问题讨论】:

  • 您缺少括号:nth-match(:text("Login"), 2)
  • @hardkoded - 我更新了它并没有显示该错误,但它仍然无法正常工作。它单击预期的文本,并且似乎继续在下一页上运行。由于下一页没有登录,因此会引发超时

标签: python playwright


【解决方案1】:

你不见了)2之后:

page.locator(':nth-match(:text("Login"), 2').click()

用途:

await page.click(':nth-match(:text("Login"), 2)');

这是另一种我觉得更简洁、更易读的方法: https://playwright.dev/docs/selectors#n-th-element-selector

所以在你的情况下

page.click('button:has-text("Login") >> nth = 2')

【讨论】:

  • 谢谢 - 我按照上面 hardkoded 的建议修复了括号。感谢您的其他建议,但这不是一个按钮。不过,我可以在其他地方使用类似的东西。
猜你喜欢
  • 2022-08-09
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
  • 2022-11-04
  • 2022-12-22
  • 2021-10-12
  • 2021-12-01
  • 2021-02-16
相关资源
最近更新 更多