【发布时间】:2020-07-31 11:55:58
【问题描述】:
我正在尝试使用 selenium 包为 python 构建一个爬虫,但我收到了这个错误:
Message: stale element reference: element is not attached to the page document
(Session info: headless chrome=83.0.4103.61)
我正在使用谷歌 colab。 我的代码是:
import selenium
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
titles=[]
for link in links:
driver.get(link)
data = driver.find_elements_by_xpath('.//a[@class = "question-hyperlink"]')
titles.append(data[0].text)
错误在data = driver.find_elements_by_xpath('.//a[@class = "question-hyperlink"]')一行
有人告诉我尝试异常处理,但我无法实现它。请帮助我如何实现它。
【问题讨论】:
-
异常处理是
try和except围绕您想要捕获的内容 - 在这里阅读 docs.python.org/3/tutorial/errors.html - 如果您尝试发布带有特定错误的代码,我们可以提供帮助: -)
标签: python selenium xpath selenium-chromedriver