I need to select an element from a drop-down menu.
The dropdown menu has two value and I need to select the second "sponsor.com"
<select tabindex="1" name="userDomain" onchange="javascript:doWFAction('ReloadUser');">
<optgroup label="Selected">
<option value="0" selected=""></option>
</optgroup>
<optgroup label="Other">
<option value="261">sponsor.com</option>
</optgroup>
</select>
What I have tried but doesnt work (I am a beginner, actually my first project)
driver.find_element_by_xpath("//select/option[@value='261']").click()
driver.find_element_by_xpath("//select[@name='userDomain']/option[text()='Other']").click()
select = Select(driver.find_element_by_name('userDomain'))
select.select_by_visible_text('Other')
select.select_by_value('261').click()
if I do this command
driver.find_element_by_name('userDomain').click()
I can click on the drop-down menu but don't know how to select sponsor.com
benkimon001benkimon001
asked Mar 12, 2019 at 8:35
-
the code where I am Searching <select tabindex="1" name="userDomain" onchange="javascript:doWFAction('ReloadUser');"> <optgroup label="Selected"> <option value="0" selected=""></option> </optgroup> <optgroup label="Other"> <option value="261">sponsor.com</option> </optgroup> </select>benkimon001– benkimon0012019年03月12日 08:36:03 +00:00Commented Mar 12, 2019 at 8:36
-
Are you sure that the select list is open and visible? Usually, you need to click on the select before clicking on the options. (If that's the problem I can make it an answer).João Farias– João Farias2019年03月12日 08:57:25 +00:00Commented Mar 12, 2019 at 8:57
1 Answer 1
Some issues with your code:
- You cannot select the
Other
optgroup element, try it manual, doesnt work, might give an exception when you try. element.select_by_value()
method will select the element, it does not return an element to click on, so you cant click on it. It will change the select for you.
The code should look like this instead:
select = Select(driver.find_element_by_name('userDomain'))
select.select_by_value('261')
answered Mar 12, 2019 at 9:39
Niels van Reijmersdal Niels van ReijmersdalNiels van Reijmersdal
32.7k4 gold badges59 silver badges125 bronze badges
default