消息:过时的元素参考:元素未附加到 Python 中的页面文档 [英] Message: stale element reference: element is not attached to the page document in Python

查看:29
本文介绍了消息:过时的元素参考:元素未附加到 Python 中的页面文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在三个不同的网络链接中运行以下代码.代码在一个网络链接上运行良好.但是,它会引发有关消息:过时的元素引用:元素未附加到页面文档"的错误消息.我查看了论坛的前两个主题(Python Selenium stale element fix如何在 Selenium 中导航到新网页?)大致相同错误消息,但没有解决问题.这是我的代码:

driver.get('https://github.com/avassalotti')贡献 = driver.find_elements_by_xpath(".//ul[@class='filter-list small']//li")打印(贡献)对于贡献项目:打印(项目)打印(项目.文本)item.click()时间.sleep(3)贡献 = driver.find_element_by_xpath(".//*[@class='f4 text-normal mb-2']").text打印(贡献)

程序适用于此链接(https://github.com/alex),但不适用于(https://github.com/agronholmhttps://github.com/avassalotti).

解决问题的任何建议.

解决方案

要检索贡献的详细信息,避免过时的元素引用,您可以使用以下解决方案:>

  • 代码块:

    from selenium import webdriverfrom selenium.webdriver.common.by import By从 selenium.webdriver.support.ui 导入 WebDriverWait从 selenium.webdriver.support 导入 expected_conditions 作为 EC年 = []选项 = webdriver.ChromeOptions()options.add_argument("开始最大化")options.add_argument('disable-infobars')driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')driver.get("https://github.com/agronholm")贡献 = WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='profile-timeline-year-list js-profile-timeline-year-list bg-white js-粘性浮动右侧 col-2 pl-5']/ul[@class='filter-list small']//li/a")))对于贡献项目:打印(item.get_attribute(innerHTML"))年.追加(item.get_attribute(href"))以年为单位:driver.get(年)print(WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='js-yearly-contributions']//h2[@class='f4 text-normal mb-2']"))).get_attribute("innerHTML"))

  • 控制台输出:

     20182017年2016年2015年2014年2013年2012年2011年2010年2009年去年贡献了 260 份2017年投稿637篇2016 年投稿 770 篇2015 年 298 次投稿2014 年 239 次投稿2013 年 101 次投稿2012 年 113 次投稿2011 年 90 份投稿2010 年 16 次投稿2009年贡献2

  • 您可以在此处找到有关 PageFactory 中的 StaleElementReference 异常的详细讨论

I have been trying to run the following code in three different weblinks. Code works fine on one weblink. However, it throws an error message about "Message: stale element reference: element is not attached to the page document". I looked into forum's two previous threads (Python Selenium stale element fix and How to Navigate to a New Webpage In Selenium?) about the same error message but didn't get sort it out the issue. Here is my code:

driver.get('https://github.com/avassalotti')
contributions = driver.find_elements_by_xpath(".//ul[@class='filter-list small']//li") 
print(contributions)
for item in contributions:
    print (item)
    print(item.text)
    item.click()
    time.sleep(3)
    contribution = driver.find_element_by_xpath(".//*[@class='f4 text-normal mb-2']").text
    print(contribution)

Program works for this link (https://github.com/alex) and does not work for (https://github.com/agronholm, https://github.com/avassalotti).

Any advice to fix the issue.

解决方案

To retrieve the details of contributions avoiding stale element reference you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    years = []
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
    driver.get("https://github.com/agronholm")
    contributions = WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='profile-timeline-year-list js-profile-timeline-year-list bg-white js-sticky float-right col-2 pl-5']/ul[@class='filter-list small']//li/a")))
    for item in contributions:
        print(item.get_attribute("innerHTML"))
        years.append(item.get_attribute("href"))
    for year in years:
        driver.get(year)
        print(WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='js-yearly-contributions']//h2[@class='f4 text-normal mb-2']"))).get_attribute("innerHTML"))
    

  • Console Output:

          2018
    
    
          2017
    
    
          2016
    
    
          2015
    
    
          2014
    
    
          2013
    
    
          2012
    
    
          2011
    
    
          2010
    
    
          2009
    
    
          260 contributions in the last year
    
    
          637 contributions in 2017
    
    
          770 contributions in 2016
    
    
          298 contributions in 2015
    
    
          239 contributions in 2014
    
    
          101 contributions in 2013
    
    
          113 contributions in 2012
    
    
          90 contributions in 2011
    
    
          16 contributions in 2010
    
    
          2 contributions in 2009
    

  • Here you can find a detailed discussion on StaleElementReference Exception in PageFactory

这篇关于消息:过时的元素参考:元素未附加到 Python 中的页面文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆