Gustavo Molina
Gustavo Molina

Reputation: 85

python selenium error stale element: element is not attached to the page document

I'm trying to get all data from a website called Correios, in this website, I need to handle some dropdowns which I'm having some issues like: It don't get all values from the first dropbox info.

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select


chrome_path = r"C:\\Users\\Gustavo\\Desktop\\geckodriver\\chromedriver.exe"

driver = webdriver.Chrome(chrome_path)
lista_x = []
driver.get("http://www2.correios.com.br/sistemas/agencias/")
driver.maximize_window()

estado_select = Select(driver.find_element_by_id('estadoAgencia'))
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'municipioAgencia')))
municipio_select = Select(driver.find_element_by_id('municipioAgencia'))
for opt in estado_select.options:
    print(opt.get_attribute('innerHTML'))
    opt.click()
    for opt2 in municipio_select.options:
        print(opt.get_attribute('innerHTML'))
        opt2.click()

driver.close()

Sometimes my code run OK, but sometimes it gives me this error:

ACRE
ACRE
ALAGOAS
ALAGOAS
Traceback (most recent call last):
  File "C:\Users\Gustavo\Desktop\insper\trabalho\Correios3.py", line 23, in <module>
    opt2.click()
  File "C:\Users\Gustavo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\Gustavo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Gustavo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Gustavo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=67.0.3396.99)
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.17134 x86_64)

What should I do?

Upvotes: 0

Views: 697

Answers (2)

Arnon Axelrod
Arnon Axelrod

Reputation: 1672

According to the traceback, the excepting is thrown from opt2.click. I guess that whenever you select an option from the estados drop-down, the available options in the municipio drop-down are updated accordingly. This means that the options that you get from municipio_select.options after selecting the 2nd estado are no longer valid and that's why you get that exception.

In order to solve that, move the line: municipio_select = Select(driver.find_element_by_id('municipioAgencia')) to after opt.click() (inside the outer loop, just before the inner for loop). This will cause the inner loop to iterate over the updated list of municipio options, and should resolve your problem.

Upvotes: 1

Sunitha
Sunitha

Reputation: 12005

Let us assume estado_select = Select(driver.find_element_by_id('estadoAgencia')) returns 2 links. You then iterate through the urls estado_select. During first iteration, (opt contains to first link), when you do opt.click(), you browser loads a new page and so the second link is now stale. So during second iteration opt.click would fail and generate the error StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

You have to do driver.navigate().back() after each click or you have to parse the new page again and build estado_select and municipio_select again

Upvotes: 0

Related Questions