小编Ros*_*ose的帖子

如何使用 Playwright Python 快速查找页面中是否存在某个元素

我有这个代码来定位链接,使用 python playwright:

nfo_link = page.locator('the xpath').get_attribute('href')
nfo_link = 'https://somesite.com' + nfo_link
logger_play.info('nfo_link: %s', nfo_link)
Run Code Online (Sandbox Code Playgroud)

如果存在则工作正常,但如果不存在则会出现错误:

waiting for selector
Run Code Online (Sandbox Code Playgroud)

我努力了:

 if (page.locator('the xpath').get_attribute('href')) == None:
       pass
 else:
       nfo_link = page.locator('the xpath').get_attribute('href')
Run Code Online (Sandbox Code Playgroud)

和其他代码,但似乎都不起作用,我想,如果发现好,如果不继续,有人可以指出我正确的方法吗?

python playwright playwright-python

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

playwright ×1

playwright-python ×1

python ×1