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?