Stale element exception when running first_selected_option in loop

24 views Asked by At

I'm currently running the following code where I try to download many .csv files from a website that contains 3 dropdowns. The second dropdown (county) is dependent on the first one (state), and the first time you make a selection in each one of them has a blank option, so I restarted it after the first selection:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import pandas as pd
import os
import time

# Initialize the Chrome WebDriver
options = Options()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=options)

driver.get('https://www2.aneel.gov.br/relatoriosrig/(S(urjkdhxs2tn25swczfh5tnpl))/relatorio.aspx?folder=sfe&report=PainelMunicipio')

t = 2

time.sleep(t)

# Select the first state and county options
state_dropdown = Select(driver.find_element("id", 'ReportViewer1_ctl04_ctl03_ddValue'))
options = state_dropdown.options
options[1].click()
time.sleep(t)

county_dropdown = Select(driver.find_element("id", 'ReportViewer1_ctl04_ctl05_ddValue'))
options = county_dropdown.options
options[1].click()
time.sleep(t)

# Now restarting it with the new dropdown
state_dropdown = Select(driver.find_element("id", 'ReportViewer1_ctl04_ctl03_ddValue'))
len_state = len(state_dropdown.options)

for i in range(0, len_state-1):
    # Select the state
    state_dropdown = Select(driver.find_element("id", 'ReportViewer1_ctl04_ctl03_ddValue'))
    state_dropdown.select_by_index(i)

    # Get the text of the selected state
    selected_option_state = state_dropdown.first_selected_option
    state_option_text = selected_option_state.text

    time.sleep(t)

    county_dropdown = Select(driver.find_element("id", 'ReportViewer1_ctl04_ctl05_ddValue'))
    len_county = len(county_dropdown.options)

    for j in range(0, len_county - 1):
        # Select the state
        county_dropdown = Select(driver.find_element("id", 'ReportViewer1_ctl04_ctl05_ddValue'))
        county_dropdown.select_by_index(j)

        # Get the text of the selected county
        selected_option_county = county_dropdown.first_selected_option
        county_option_text = selected_option_county.text

        time.sleep(t)

        # Click the button to generate the report
        report_button = driver.find_element("id", 'ReportViewer1_ctl04_ctl00')
        report_button.click()

        time.sleep(t+10)

        # Download the report in Excel format
        export_button = driver.find_element("id", 'ReportViewer1_ctl06_ctl04_ctl00_Menu')
        driver.execute_script("$find('ReportViewer1').exportReport('CSV');", export_button)

        time.sleep(t)

driver.quit()

The problem that I'm facing is the following: when I run the county loop for the second time, I receive an stale exception error when I try to run the code

selected_option_county = county_dropdown.first_selected_option"

(so I imagine it will alson happen with the state loop). I've tried to update the selection of the dropdown adding the first line of the loop

county_dropdown = Select(driver.find_element("id", 'ReportViewer1_ctl04_ctl05_ddValue'))

, and that fixed the same stale error happening when I ran the

county_dropdown.select_by_index(j) 

for the second time. But for some reason I'm not being able to recover the first_selected_option. Also, I tried to inspect the HTML and didn't find any changes when selecting different options in the dropdown.

0

There are 0 answers