我用Python的硒刮網站, 但我履帶停止,因爲一個例外:如何忽略Selenium中的異常?
StaleElementReferenceException
:消息:過時的元素參考:元素沒有被附加到頁面文件
即使未連接元素,我如何繼續爬行?
UPDATE
我我的代碼更改爲:
try:
libelle1 = prod.find_element_by_css_selector('.em11')
libelle1produit = libelle1.text # libelle1 OK
libelle1produit = libelle1produit.decode('utf-8', 'strict')
except StaleElementReferenceException:
pass
,但我有這個例外
NoSuchElementException: Message: no such element
我也試過這樣:
try:
libelle1 = prod.find_element_by_css_selector('.em11')
libelle1produit = libelle1.text # libelle1 OK
libelle1produit = libelle1produit.decode('utf-8', 'strict')
except :
pass
考慮發佈一些導致問題的代碼。否則,提供的下一個最好的事情就是簡單地用try-except或者with with supress' context manager來忽略異常 – sytech