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?
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
...
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
发布评论
评论(1)
您可以将playwright的 loacators 选择所有标签(或其他依赖于按钮或其他依赖其他标签)您要寻找的)。 上运行您想要的检查
通过获得的定位器进行迭代,并在每个编辑 :
阅读更多,看起来您实际上可以使用
试用
单击。page.click(选择器,试用= true)
在您跳跃方法之前的外观。剧作家还在每次点击和其他事件之前href =“ https://playwright.dev/docs/api/class-page#page-click” rel =“ nofollow noreferrer”>不可行。像这样:
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
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: