Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium iterate through options in dropdown select

I'm using selenium in python, and I have a dropdown that I'm trying to select from. Essentially, I just want to iterate through all the options, like so:

select first option 
  submit page 
  \\ do stuff
select second option 
  submit page 
  \\ do stuff
select third option 
  submit page 
  \\ do stuff
etc...

I know it's possible to do this if you know what each of the option values are (you just create a list of those values), but is there a way of just iterating through all the options when you don't know what the option values are?

Thanks!

like image 714
chris Avatar asked Aug 31 '26 16:08

chris


2 Answers

You can get list of all the options using select.options. Than you can select options using index.

select = Select(driver.find_element_by_id("dropDown"))
options = select.options
for index in range(0, len(options) - 1):
    select.select_by_index(index)
    # do stuff
like image 64
Guy Avatar answered Sep 03 '26 07:09

Guy


You can simply iterate through all options:

select = Select(driver.find_element_by_id("someId"))

for opt in select.options:
    # for example
    print(opt.text)
    opt.click()
like image 37
Mykola Zotko Avatar answered Sep 03 '26 09:09

Mykola Zotko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!