The easiest way that I have found was to do something along the lines of:
el = driver.find_element_by_id('id_of_select')
for option in el.find_elements_by_tag_name('option'):
if option.text == 'The Options I Am Looking For':
option.click() # select() in earlier versions of webdriver
break
This may have some runtime issues if there are a large number of options, but for us it suffices.
Also this code will work with multi-select
def multiselect_set_selections(driver, element_id, labels):
el = driver.find_element_by_id(element_id)
for option in el.find_elements_by_tag_name('option'):
if option.text in labels:
option.click()
Then you can transform the following field
# ERROR: Caught exception [ERROR: Unsupported command [addSelection | id=deformField7 | label=ALL]]
Into this call
multiselect_set_selections(driver, 'deformField7', ['ALL'])
Multiple selection errors like the following:
# ERROR: Caught exception [ERROR: Unsupported command [addSelection | id=deformField5 | label=Apr]]
# ERROR: Caught exception [ERROR: Unsupported command [addSelection | id=deformField5 | label=Jun]]
Will be fixed with a single call:
multiselect_set_selections(driver, 'deformField5', ['Apr', 'Jun'])
click()on the default<option>, which made it look like nothing had changed. – John Keyes Jul 07 '11 at 10:30option.click(), but it does work withoption.select(). In the latest webelement.py source,selecthas been removed, andclickmust be used in it’s place. It will be interesting to see if my test works with the latest release. – John Keyes Jul 07 '11 at 10:58