1.用try尝试点击,点击不到会报NoSuchElementException,捕获到后不会报错,继续进行
从selenium.common.exceptions导入 NoSuchElementException
from selenium.common.exceptions import NoSuchElementException
try:
print("尝试点击xxx")
driver.find_element('xpath', '//*[@text="xxx"]'.click()
except NoSuchElementException:
print("没有找到xxx")
2.先用一个列表去定位,如果有值就点击,没有就进行下一步
result = driver.find_element('xpath', '//*[@text="xxx"]')
if len(result) > 0:
print('点击xxx')
result[0].click()
else:
print('没有xxx')