0
我正在使用硒在python 2.7中開發一個刮板。我面臨的主要問題是我的程序在沒有發現這種元素異常之後立即終止。 我已經嘗試了幾種解決方案,但是沒有任何效果。就像使用try catch等 這裏是我試過的代碼。程序在沒有這種元素髮現異常後立即退出
def check_exists_by_xpath(xpath):
try:
webdriver.find_element_by_xpath(xpath)
except NoSuchElementException:
return False
return True
即使元素不存在,我也希望程序恢復。 任何人都可以提出解決方案如何做到這一點? 我已經嘗試了許多其他的東西,但沒有任何工作。
錯誤消息:
Traceback (most recent call last):
File "bot2.py", line 22, in <module>
driver.find_element_by_xpath(xpath)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 295, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 756, in find_element
'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="react-root"]/section/usama"}
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 10.0.14393 x86_64)
你有一些錯誤信息嗎?你可以和我們分享嗎? –
添加評論太長,所以我在 –
這個問題中編輯了它,這正是你需要做的。評論是不好的。 –