I am new to python and I am trying to select from a "cascading" drop-down menu. first, you select the state in the website, and then it gives you a list of municipalities to choose from. Each state has a different list of municipalities.

I tried this code, where "estados" are the states and "municipios" are the municipalities.:

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(ChromeDriverManager().install())

estados = Select(driver.find_element_by_name('estado'))
for index in range(1, len(estados.options)):
    estados = Select(driver.find_element_by_name('estado'))
    municipios = Select(driver.find_element_by_name('municipio'))
    for index_2 in range(1, len(municipios.options)):
        municipios = Select(driver.find_element_by_name('municipio'))

It works until the second state. when the program tries to access the list of municipalities for the second state, I get this error:

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

  • Pedro, at which line are you accessing the stale element? – João Farias Oct 15 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.