0

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())
driver.get('http://lixoes.cnm.org.br/')

driver.find_element_by_xpath('//*[@id="header"]/div[1]/a').click()
estados = Select(driver.find_element_by_name('estado'))
for index in range(1, len(estados.options)):
    estados = Select(driver.find_element_by_name('estado'))
    estados.select_by_index(index)
    municipios = Select(driver.find_element_by_name('municipio'))
    time.sleep(1)
    for index_2 in range(1, len(municipios.options)):
        municipios = Select(driver.find_element_by_name('municipio'))
        municipios.select_by_index(index_2)
        driver.find_element_by_xpath('//*[@id="header"]/div[2]/div[2]/form/input').click()
        time.sleep(1)
    time.sleep(2)

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.