KpsLok
KpsLok

Reputation: 903

Selenium is not selecting the correct value

Im using selenium to test a web page which has a select option and one input type text.

Manually and using the selenium IDE, it works correctly, but when I export the test case to Java Junit, i can see the dropdown click, but selenium is not selecting the value, it is just expanding the dropdown.

What can i do?

Lets check my code:

Select dropdown = new Select(driver.findElement(By.id("type")));
dropdown.selectByVisibleText("Celcius");

Consider my form, like:

<form action="doit">
    <select name="type" id = "type">
        <option value = "fail"> Fail </option>
        <option value = "celcius"> Celcius </option>        
    </select>
    <input type="number" name="num" id="num">
</form>

Upvotes: 1

Views: 252

Answers (2)

Anupam Singh
Anupam Singh

Reputation: 62

driver.findElement(By.id("type")).sendkeys("Celcius");

Upvotes: 1

Eby
Eby

Reputation: 396

Sometimes this method wont work. This may happen if the text between the option tags have spaces before and after the tags. Eg.

1.<option> Fail </option> 2.<option>Fail</option>

In the above example 1 and 2 are different. So you can use like,

driver.findElement(By.id("type")).click();     // this will expand the list
driver.findElement(By.xpath("//select[@id='type']/option[contains(text(),'Celcius')]")).click();

Also try to click directly. It work if the element is vivible,

driver.findElement(By.xpath("//select[@id='type']/option[contains(text(),'Celcius')]")).click();

Upvotes: 1

Related Questions