Python Selenium查找此元素或此元素
想知道是否有办法在两个实例中找到相同的元素。我遇到了这个问题,它有时找到元素,有时找不到(不是时间问题) 比如:Python Selenium查找此元素或此元素,python,python-2.7,selenium,selenium-webdriver,Python,Python 2.7,Selenium,Selenium Webdriver,想知道是否有办法在两个实例中找到相同的元素。我遇到了这个问题,它有时找到元素,有时找不到(不是时间问题) 比如: driver.find_element_by_xpath("Element xpath 1" or "Element xpath 2") 解决这一问题的一种方法是遵循以下步骤: 另一种方法是修改表达式,并使用“or”(管道字符)将两个xpath表达式连接成一个表达式: 请注意,“元素xpath 1”或“元素xpath 2”始终计算为“元素xpath 1”。 from seleniu
driver.find_element_by_xpath("Element xpath 1" or "Element xpath 2")
解决这一问题的一种方法是遵循以下步骤: 另一种方法是修改表达式,并使用“or”(管道字符)将两个xpath表达式连接成一个表达式: 请注意,
“元素xpath 1”或“元素xpath 2”
始终计算为“元素xpath 1”
。
from selenium.common.exceptions import NoSuchElementException
try:
element = driver.find_element_by_xpath("Element xpath 1")
except NoSuchElementException:
element = driver.find_element_by_xpath("Element xpath 2")
element = driver.find_element_by_xpath("//path1/to/element | //path2/to/element")