0ddajw
Last Updated: February 25, 2016
·
1.035K
· mokagio
Question mark avatar

Get an <option> from a <select> with Python Selenium WebDriver

I recently started testing a Django app with Selenium and it took me while, as the Python noob I am, to find out how to click on an option from a select.
This link helped me a lot.

Here's the snippet:

# Get the select
select = browser.find_element_by_name('select-name')

# Get the option based on its text
option
for opt in select.find_elements_by_tag_name('option'):
    if opt.text == 'Something'
        option = opt

# Do whatever you want with the option
option.click()

Note that if you are using some kind of subcategory system in your select option list based on indentation than == will not be enough to get your option. You can use something like:

if 'Something' in opt.text
     option == opt
Say Thanks
Respond