chris chris - 2 years ago 104
Python Question

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

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?


Guy Guy
Answer Source

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):
    # do stuff
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download