硒的Python StaleElementReferenceException
问题描述:
我想下载所有关于使用Selenium用Python将Chrome作为浏览器,但每次会话与此消息结束时间网页上的PDF格式:硒的Python StaleElementReferenceException
StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=52.0.2743.116)
(Driver info: chromedriver=2.22.397933
这是代码:
def download_pdf(self):
current = self.driver.current_url
lista_link_temp = self.driver.find_elements_by_xpath("//*[@href]")
for link in lista_link_temp:
if "pdf+html" in str(link.get_attribute("href")):
tutor = link.get_attribute("href")
self.driver.get(str(tutor))
self.driver.get(current)
请帮助我..我只是试着拉姆达,显性和隐性等待
感谢
答
只要在循环中调用self.driver.get()
,元素列表中的所有其他元素将变为陈旧。尝试从内容收集href
属性,然后再探访他们:
def download_pdf(self):
current = self.driver.current_url
lista_link_temp = self.driver.find_elements_by_xpath("//*[@href]")
pdf_hrefs = []
# You could do this part with a single line list comprehension too, but would be really long...
for link in lista_link_temp:
href = str(link.get_attribute("href"))
if "pdf+html" in href:
pdf_hrefs.append(href)
for h in pdf_hrefs:
self.driver.get(h)
self.driver.get(current)
太棒了!有用!谢谢..我失去了希望.. – LBdoc
@Lddoc很高兴听到它! – elethan