首页 > 解决方案 > Selenium Webdriver Python - 检查元素是否可见/检测到/存在

问题描述

我想检查 Selenium Web 驱动程序对于以下 XPath 是否可见/存在/检测到元素:

//*[@data-animate-modal-popup="true"]

每当该元素可见/存在/检测到时,是否有任何 Selenium 函数返回 TRUE 或 FALSE?

上次我使用以下 IF - Else

phone_number_invalid = driver.find_element_by_xpath('//*[@data-animate-modal-popup="true"]')
if phone_number_invalid:
     code here .....

find_element_by_xpath每当找不到元素时,它总是会引发错误。只要元素可见/存在/检测到,我只想获得 TRUE 或 FALSE。

谢谢你。

标签: pythonseleniumselenium-webdriver

解决方案


  • 为方便起见,您为什么不使用扩展程序。
  • 使用铬:
  • 使用“Xpath helper”检查元素:

https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en

  • 使用“Xpath finder”查找元素:

https://chrome.google.com/webstore/detail/xpath-finder/ihnknokegkbpmofmafnkoadfjkhlogph?hl=en


推荐阅读