陈旧;元素不再附加到 DOM,不在当前框架上下文中,或者文档已刷新 [英] is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
本文介绍了陈旧;元素不再附加到 DOM,不在当前框架上下文中,或者文档已刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我使用 selenium 抓取链接.我可以用循环打印我的链接,但我无法导航到它们,因为我收到以下错误:
So im scraping links using selenium. I can print my links with my loop but I cant navigate to them because I get the following error:
selenium.common.exceptions.StaleElementReferenceException:消息:元素引用过时;元素不再附加到 DOM,不在当前框架上下文中,或者文档已刷新
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.famousgraphicdesigners.org/")
links = driver.find_elements_by_xpath('//*[@id="pages-2"]/div/ul/li/a')
links_total = len(links)
print("Found", links_total, "total links.\n")
for i in links:
# print(i.get_attribute('href')) # This works
driver.get(i.get_attribute('href')) # This doesnt work
driver.quit()
推荐答案
当您导航到新页面时,先前定位的元素将变得陈旧,在这种情况下,links
中的元素,因此您可以't 访问 href
属性.将所有 href
保留在字符串列表中并对其进行迭代
When you navigate to a new page the previously located elements become stale, in that case the elements in links
, so you can't access the href
attribute. Kee p all the href
s in a list of strings and iterate over it
links = driver.find_elements_by_xpath('//*[@id="pages-2"]/div/ul/li/a')
links_hrefs = [link.get_attribute('href') for link in links]
for i in links_hrefs:
driver.get(i)
这篇关于陈旧;元素不再附加到 DOM,不在当前框架上下文中,或者文档已刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文