Home > database >  Selenium: store scraped data in a list
Selenium: store scraped data in a list

Time:07-14

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"  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

CodePudding user response:

Yes, You can use list comprehension

years =[x.text.strip() for x in  WebDriverWait(driver, 10).until(
       EC.presence_of_element_located((By.ID, "filter")))]
print(years)

CodePudding user response:

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]
  • Related