简体   繁体   English

硒没有选择正确的值

[英]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. 手动并使用selenium IDE可以正常工作,但是当我将测试用例导出到Java Junit时,我可以看到下拉单击,但是selenium没有选择该值,它只是在扩展下拉列表。

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> 1. <option> Fail </option> 2. <option>Fail</option>

In the above example 1 and 2 are different. 在上面的示例中1和2不同。 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( “摄氏度”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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