【问题标题】:How to check whether a link is clickable in Playwright?如何检查 Playwright 中的链接是否可点击?
【发布时间】:2022-11-09 23:52:51
【问题描述】:

考虑以下屏幕截图:

modal

在这里,只有按钮 I acceptMore options 是可点击的。模态背景可防止对导航链接的任何点击。是否可以使用 Playwright 仅选择当前视口中的链接,并且如果在其边界框内发生单击事件,则会收到点击事件?

谢谢

【问题讨论】:

  • 嘿,我也在寻找这个解决方案。你解决了吗?

标签: playwright


【解决方案1】:

您可以使用剧作家的actionability checks 组合将链接/按钮缩小到仅可点击的链接/按钮。从locators 开始以选择所有标签(或按钮或其他内容,具体取决于您要查找的内容)。遍历您获得的定位器并在每个定位器上运行您想要的检查

locators = page.locator(selector)
locator_count = await locators.count()
for index in range(0, locator_count):
    locator = locators.nth(index)
    if await locator.is_visible() and await locator.is_enabled():
        # do your action
        ...

那就是你跳跃之前的样子。 Playwright 还在每次点击和其他事件之前does these checks,所以如果您只是想对有效元素执行操作,您只需点击try 并处理错误(如果它是not actionable)。像这样:

locators = page.locator(selector)
locator_count = await locators.count()
for index in range(0, locator_count):
    locator = locators.nth(index)
    try:
        await locator.click()
    except TimeoutError:
        # handle error. likely just skip this locator
        continue

【讨论】:

    猜你喜欢
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 2023-03-27
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多