1

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

  • 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 Mar 12 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 Mar 12 at 8:57
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')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.