【发布时间】:2018-07-13 12:19:02
【问题描述】:
我在 python 中结合 selenium 创建了一个刮板,用于从网站收集一些信息。但是,我面临的问题是在收集单个潜在客户后,刮板会抛出错误element is not attached to the page document。
考虑以下代码:
for loop滚动的名称有 20 个,并且刮板应该点击每个名称。点击名字后,它会在新页面中等待文档可用。
在该页面的右上角有一个显示更多按钮,单击该按钮可解开隐藏的信息。 (它仍然停留在第二页,只是显示了一个新信息)。
一旦信息显示,scraper 就会成功收集。
然后它应该返回到循环开始的起始页面并点击下一个名称。但是,它不会单击下一个名称,而是会引发以下错误(在
link.click()行上)。
我试图通过使用wait.until(EC.staleness_of(item)) 来消除陈旧元素错误,但它不起作用。
for link in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"div.presence-entity__image"))):
link.click() #error thrown here
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"button[data-control-name='contact_see_more']"))).click()
item = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,".pv-contact-info__ci-container a[href^='mailto:']")))
print(item.get_attribute("href"))
driver.execute_script("window.history.go(-1)")
wait.until(EC.staleness_of(item))
我遇到的错误:
line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
我试图描绘正在发生的事情。对此的任何帮助将不胜感激。
【问题讨论】:
标签: python python-3.x selenium selenium-webdriver web-scraping