如何检查剧作家中是否可以单击链接?

发布于 2025-01-30 14:17:49 字数 247 浏览 1 评论 0 原文

考虑以下屏幕截图:

modal

在这里,只有按钮我接受 更多选项可单击。模态背景可以防止对导航链接的任何单击。是否可以使用剧作家仅选择当前视图中的链接,并且会在其边界框中发生一个点击事件吗?

谢谢

Consider the following screenshot:

modal

Here, only buttons I accept and More options are clickable. The modal background prevents any clicks to the navigation links. Is it possible to use Playwright to select only links that are in current viewport and that would receive a click event should one occur within their bounding box?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿越时光隧道 2025-02-06 14:17:49

您可以将playwright的 loacators 选择所有标签(或其他依赖于按钮或其他依赖其他标签)您要寻找的)。 上运行您想要的检查

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

通过获得的定位器进行迭代,并在每个编辑 :
阅读更多,看起来您实际上可以使用 试用单击。
page.click(选择器,试用= true)

在您跳跃方法之前的外观。剧作家还在每次点击和其他事件之前href =“ https://playwright.dev/docs/api/class-page#page-click” rel =“ nofollow noreferrer”>不可行。像这样:

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

You can use a combination of playwright's actionability checks to narrow down links / buttons to only those that are clickable. Start with locators to select all tags (or buttons or something else depending what you're looking for). Iterate through the locators you get and run the checks you want on each

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

Edit:
Reading more and it looks like you can actually do those actionability checks using the trial option with click.
page.click(selector, trial=True)

Thats the look before you leap method. Playwright also does these checks before each click and other events, so if you're just looking to perform an action on a valid element, you can just try a click and handle the error if it's not actionable. Like so:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文