0

I am trying to extract the numbers from the below page but always showed an exception, I tried various solutions but still get StaleElementReferenceException, here is my code..

I tried sleep and wait...., what is the problem?, please

options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:/Users/Lenovo/PycharmProjects/pythonProject/chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)

driver.get("https://ec.europa.eu/ecat/category/en/5/dishwasher-detergents")
products = []

num_pages = 7
step = 0

while (step < num_pages):
    
    try:
        time.sleep(2)
        elm = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//*[@id='producersTableId']/tbody/tr/td[4]")))
        time.sleep(2)
        products.extend(elm)
        for i in products:
            print(i.text) # the exception appreas here
        elm = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//*[@id='producersTableId']/tbody/tr/td[4]")))

        next_Butt =  wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='producersTableId_next']/a")))
        driver.execute_script("arguments[0].click();", next_Butt)
        time.sleep(2)
        step += 1
    except exceptions.StaleElementReferenceException as e :
        print(e)
        pass
print(len(products))
        
    

    
   
simpleApp
  • 2,885
  • 2
  • 10
  • 19
  • you are getting the exception `StaleElementReferenceException` because that element is not present on the page that the driver is currently serving. may i know why you are looping this `for i in products` ? because you are looping page 1 elements again! if you loop the elm then it should be okay. – simpleApp Jan 12 '23 at 17:29
  • I looped to display the retrieved elements from products, i tried with elm & gives me the same sception – Lina_Yah Jan 12 '23 at 17:52
  • when i am using this code `for i in elm: print(i.text)` , unable to produce StaleElementReferenceException. – simpleApp Jan 12 '23 at 20:49
  • Ah my bad, I did: for elm in products, but with this, it works thank you – Lina_Yah Jan 12 '23 at 21:16

0 Answers0