Selenium中没有提供原生的方法判断元素是否存在,一般我们可以通过定位元素+异常捕获的方式判断。Python示例代码如下: # 没有发生异常,表示在页面中找到了该元素,返回Truefrom selenium.common.exceptions import NoSuchElementException
# 判断元素是否存在 def isElementPresent(self, by, value): try: element =self.driver.find_element(by=by, value=value) # 发生了NoSuchElementException异常,说明页面中未找到该元素,返回False except NoSuchElementException as e: return False else: # 没有发生异常,表示在页面中找到了该元素,返回True return True 下面是不使用方法运
|
|
来自: 福兮007 > 《python-selenium自动化》