I have a SELECT list
<select size="4" name="ctl00$ContentPlaceHolder1$lstAvailableClients" id="ctl00_ContentPlaceHolder1_lstAvailableClients" style="height:150px;width:250px;">
<option value="2780">2780 - R W M (self)</option>
<option value="2714">2714 - Robert (self)</option>
</select>
Currently I just select the first option like this:
el = driver.find_element('id','ctl00_ContentPlaceHolder1_lstAvailableClients')
for option in el.find_elements(By.TAG_NAME,"option"):
option.click() # select() in earlier versions of webdriver
break
But I can get the count using
el.find_elements(By.TAG_NAME, "option").count
I could ask the user to select an option 1-2 to proceed.
Is there a way to get the option count and then get the option.click()
to select that index to click?
There's a built-in class in Selenium called Select()
that makes interacting with SELECT elements much easier. Here are a few simple examples using the HTML you provided.
# find the SELECT element and turn it into a Select class
select = Select(driver.find_element(By.ID, "ctl00_ContentPlaceHolder1_lstAvailableClients"))
# find the count of OPTIONs
num_options = len(select.options)
# three different ways to select the first OPTION
select.select_by_index(1)
select.select_by_value("2780")
select.select_by_visible_text("2714 - Robert (self)")
There's a lot more functionality available, see the docs.