I'm using Selenium-webdriver with Java from the MavenProject. Version 2.53.0.

This is the HTML of the dropdown field:

<div class="col-lg-7 col-md-7">
  <select id="user_contract_type_of_contract" name="user_contract[type_of_contract]" class="form-control input-sm">
    <option value="780">N/A</option>
    <option value="775">Contract onbepaalde duur</option>
    <option value="774">Contract bepaalde duur</option>
    <option value="778">Interim</option>
    <option value="776">IBO</option>
    <option value="777">Industrieel leerling</option>
    <option value="781">Stagiair</option>
    <option value="779">Jobstudent</option>
    <option value="782">Vervangingscontract</option>

I'm trying to select the second option from the field named 'Contract onbepaalde duur'. I've tried 2 things so far, neither worked:

Attempt 1:

Thread.sleep(2000); // I wait here just to play it safe

Attempt 2:

Thread.sleep(2000); // I wait here just to play it safe
driver.findElement(By.xpath("//*[text()='Contract onbepaalde duur']")).click(); 

Current output:

What both attempts do is, they click on the field. The dropdown opens and it waits a few seconds it selects the second option but somehow it doesn't seem to click the option (it does get selected) but then it hops to the next field to fill that one in. BUT instead of saving the selected field it saves the first default option 'N/A'.


What am I doing wrong that it doesn't save the selected/clicked item?

  • select option is not working on div elements. Error Message : "selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on <select> elements, not on <div>"
    – vmpk
    Commented Jan 30, 2019 at 14:18

2 Answers 2


Use Select method in WebDriver-

WebElement contractType = driver.findElement(By.xpath("//select[@id='user_contract_type_of_contract']"));

Select dropdownField = new Select(contractType);
dropdownField.selectByVisibleText("Contract onbepaalde duur");

// Select by using index starting from 0 to ...you have to select 2nd option so take as index-1
  • use different xpath-as- By.xpath (//*[@id='user_contract_type_of_contract']") I think it w'll definitely works,- if it is not works let me know- Always try to avoid "Thread.sleep" Use implicitly wait instead of this
    – Bharat Mane
    Commented Apr 27, 2016 at 10:09
  • Works like a charm
    – Decypher
    Commented Apr 27, 2016 at 10:10

You can also select dropdown option by value

WebElement userContractType = driver.findElement(By.xpath("//select[@id='user_contract_type_of_contract']"));

Select selectValue = new Select(userContractType);
  • Why not use By Id?
    – Paul Muir
    Commented Apr 27, 2016 at 13:35
  • You can use By Id also, for some case Id is not available so using xpath is right option.
    – amitsahu86
    Commented Apr 27, 2016 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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