from selenium.common.exceptions import NoSuchElementException
# 封装一个函数,用来判断属性值是否存在
def isElementPresent(self, by, value):
"""
用来判断元素标签是否存在,
"""
try:
element = self.driver.find_element(by=by, value=value)
# 原文是except NoSuchElementException, e:
except NoSuchElementException as e:
# 发生了NoSuchElementException异常,说明页面中未找到该元素,返回False
return False
else:
# 没有发生异常,表示在页面中找到了该元素,返回True
return True
if self.isElementPresent("id", 'form:PORLocation0'):
fromCity = self.driver.find_element_by_xpath("//span[@id='form:PORLocation0']").text
else:
print("元素值不存在")
可以关注我的公众号:技术趣谈