3

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>
  </select>
</div>

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:

driver.findElement(By.id("user_contract_type_of_contract")).click();
Thread.sleep(2000); // I wait here just to play it safe
driver.findElement(By.xpath("//*[@id=\"user_contract_type_of_contract\"]/option[2]")).click();

Attempt 2:

driver.findElement(By.id("user_contract_type_of_contract")).click();
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'.

Question:

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

1
  • 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
    Jan 30, 2019 at 14:18

2 Answers 2

5

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");

OR
// Select by using index starting from 0 to ...you have to select 2nd option so take as index-1
dropdownField.selectByindex(1);
2
  • 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 Apr 27, 2016 at 10:09
  • Works like a charm
    – Decypher
    Apr 27, 2016 at 10:10
0

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);
selectValue.selectByValue("775");
2
  • Why not use By Id?
    – Paul Muir
    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
    Apr 27, 2016 at 14:16

Your Answer

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

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