我将使这个问题简单化:
我有一个if语句:
if self.driver.find_element_by_xpath('//*[contains(@id, "mission_countdown")]'):
我得到这个错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(@id, "mission_countdown")]"}
为什么我得到这个?我的意思是它的if语句,如果它在屏幕上
感谢您的帮助
最佳答案
find_element_by_xpath
可以返回
WebElement
(如果找到)NoSuchElementException
(如果未找到)如果您正在搜索可评估为boolean以便在
if
/else
块中使用的输出,请尝试find_elements
:if self.driver.find_elements_by_xpath('//*[contains(@id, "mission_countdown")]'):
print('At least one element was found')
else:
print('No elements found')
关于python - Selenium (如果在屏幕上定位无法找到元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54933394/