Message: stale element reference: element is not attached to the page document in Python

后端 未结 1 1770
既然无缘
既然无缘 2020-12-12 05:51

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

相关标签:
1条回答
  • 2020-12-12 06:24

    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:\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"))
      
    • 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

    0 讨论(0)
提交回复
热议问题