【问题标题】:Message: stale element reference: element is not attached to the page document in Python消息:过时的元素引用:元素未附加到 Python 中的页面文档
【发布时间】:2019-02-03 23:47:11
【问题描述】:

我一直在尝试在三个不同的网络链接中运行以下代码。代码在一个网络链接上运行良好。但是,它会引发有关“消息:过时元素引用:元素未附加到页面文档”的错误消息。我查看了论坛之前的两个线程(Python Selenium stale element fixHow to Navigate to a New Webpage In Selenium?)关于相同的错误消息,但没有解决这个问题。 这是我的代码:

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)

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

任何解决问题的建议。

【问题讨论】:

  • 这是您必须使用 WATIR 的完美用例,一旦它过时,它就会完美地恢复元素。我在 WATIR 中运行它,它运行良好。如果您的项目还没有走得太远,您可以使用位于 Ruby Selenium Binding 顶部的 WATIR。
  • 试试for item in driver.find_elements_by_xpath(".//ul[@class='filter-list small']//li"),这样每次迭代都会更新列表
  • @Andersson 我不懂 Python,但是 Ruby 也有类似的 for 循环,它不会每次都执行 driver.find_elements,它开始在集合上执行由driver.find_elements 返回,所以它肯定会过时。
  • @Rajagopalan,是的。当然。那是仓促的决定:)

标签: python selenium selenium-webdriver webdriverwait staleelementreferenceexception


【解决方案1】:

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

  • 代码块:

    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:\Utility\BrowserDrivers\chromedriver.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"))
    
  • 控制台输出:

          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
    
  • 在这里你可以找到关于StaleElementReference Exception in PageFactory的详细讨论

【讨论】:

    猜你喜欢
    • 2019-09-30
    • 2022-08-17
    • 2018-10-23
    • 2022-01-21
    • 2021-05-10
    • 2021-12-19
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多