6

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!

1
  • Does Python have something like select_by_id? Which is a number? Commented Feb 22, 2016 at 17:10

3 Answers 3

10

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
Sign up to request clarification or add additional context in comments.

Comments

3

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()

Comments

1

thanks for the answer, question, shouldn't it be

select = Select(driver.find_element_by_id("dropDown"))
options = select.options
for index in range(0, len(options)):
    select.select_by_index(index)
    # do stuff   

1 Comment

The only difference between this and the other answer is the ` - 1` on line 3. Please explain the difference and why it is important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.