Selenium: store scraped data in a list

Issue

I’m using selenium for the first time and I’m having some problem get the data tha way I need. Basically, I’d like to get all the years stored in a dropdown menu, this is pretty much the code:

<select name="order" id="filter" tabindex="0" class="dropdown" aria-pressed="false">

        <option data-ref="m3" value="months6" selected="">
            last 6 months
        </option>
    
        <option data-ref="y2022" value="year-2022">
            2022
        </option>
    
        <option data-ref="y2021" value="year-2021">
            2021
        </option>
    
        <option data-ref="y2020" value="year-2020">
            2020
        </option>    

    
</select>

This is my code to scrape the information:

years = WebDriverWait(driver, 10).until(
       EC.presence_of_element_located((By.ID, "filter"))
)
print(years.text)

This is what I’m getting using print(years.text)

'    \n         last 6 months\n    \n              2022\n    \n         2021\n    \n         2020\n    \n         Ordini Archiviati\n    '

Is there a way to store the information in a list? And not in a string like this?
Thank you all

Solution

You should use Select class for <select> tags

from selenium.webdriver.support.ui import Select

years = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "filter")))
select = Select(years)
options = [option.text for option in select.options]

Answered By – Guy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published