我使用Python 来抓取网站,但是我的爬虫停止了,因为有一个例外:
StaleElementReferenceException:消息:陈旧的元素引用:元素没有附加到页面文档
即使元素没有附加,我如何继续爬行?
更新
我将代码更改为:
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发布于 2016-07-02 02:20:16
它看起来像是浏览器呈现引擎或Javascript引擎正在使用该元素,并且它正在阻止对该元素的其他外部操作。您可以在一段时间后尝试访问它。如果在较长时间内无法访问它,则会引发异常。给出了这里的一些较好的例子。
发布于 2016-06-29 17:47:14
在产生该错误的代码块周围放置一个“尝试--除了”块。
发布于 2016-06-29 18:11:07
更具体地讲约翰·戈登在说什么。处理StaleElementReferenceException常见的selenium异常并忽略它:
from selenium.common.exceptions import StaleElementReferenceException
try:
element.click()
except StaleElementReferenceException: # ignore this error
pass # TODO: consider logging the exceptionhttps://stackoverflow.com/questions/38106678
复制相似问题