【问题标题】:Playwright - Element not Clickable剧作家 - 不可点击的元素
【发布时间】:2022-02-04 03:59:52
【问题描述】:

我正在尝试单击页面上显示的图标,但是尽管尝试了我能想到的所有方法,但我仍然收到以下错误:

waiting for selector "[aria-label="Comment"]"
  selector resolved to 2 elements. Proceeding with the first one.
  selector resolved to hidden <svg role="img" width="24" height="24" fill="#8e8e8e"…>…</svg>
attempting click action
  waiting for element to be visible, enabled and stable
  element is visible, enabled and stable
  scrolling into view if needed
  element is not visible
retrying click action, attempt #1
  waiting for element to be visible, enabled and stable
  element is visible, enabled and stable
  scrolling into view if needed
  done scrolling
  element is not visible
retrying click action, attempt #2
  waiting 20ms
  waiting for element to be visible, enabled and stable

等等等等等等

所以看起来该元素是隐藏的,虽然我在 html 中看不到任何迹象....附加。

非常感谢任何帮助。

enter image description here

【问题讨论】:

  • 也许您有两个带有“评论”标签的元素?
  • 是的,但你可以看到它选择了第一个,这是正确的。
  • 更新:这实际上是我需要选择的第二个,有没有简单的方法可以做到这一点?
  • 改进选择器,以便您选择第二个元素。

标签: playwright


【解决方案1】:

短版

page.locator('svg[aria-label="Comment"] >> nth=1').click();

【讨论】:

    【解决方案2】:

    您可以使用nth定位器的方法来获取第二个元素:

    const comment = page.locator('svg[aria-label="Comment"]').nth(1);
    
    await comment.waitFor();
    await comment.click();
    

    请记住,这是从零开始的编号。

    【讨论】:

    • 嗨,非常感谢@Yevhen,这似乎有效,因为它找到了元素,尽管定位器没有点击方法,但之后我如何点击它?
    • 太棒了。实际上,定位器提供了click 方法。我在代码块中添加了点击操作。不要忘记接受答案。
    • 非常感谢,我会尝试并回复您。
    猜你喜欢
    • 2022-01-04
    • 2020-11-16
    • 2020-09-24
    • 2022-12-21
    • 2021-06-25
    • 1970-01-01
    • 2021-11-24
    • 2021-10-20
    • 2022-08-02
    相关资源
    最近更新 更多