简体   繁体   中英

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>

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

driver.findElement(By.id( “类型”))的SendKeys( “摄氏度”);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM