Java Selenium没有选择正确的值

Java Selenium没有选择正确的值,java,selenium,Java,Selenium,Im使用selenium测试一个网页,该网页有一个选择选项和一个输入类型文本 手动使用selenium IDE,它工作正常,但是当我将测试用例导出到Java Junit时,我可以看到下拉菜单单击,但是selenium没有选择值,它只是扩展下拉菜单 我能做什么 让我们检查我的代码: Select dropdown = new Select(driver.findElement(By.id("type"))); dropdown.selectByVisibleText("Celcius"); 考虑

Im使用selenium测试一个网页,该网页有一个选择选项和一个输入类型文本

手动使用selenium IDE,它工作正常,但是当我将测试用例导出到Java Junit时,我可以看到下拉菜单单击,但是selenium没有选择值,它只是扩展下拉菜单

我能做什么

让我们检查我的代码:

Select dropdown = new Select(driver.findElement(By.id("type")));
dropdown.selectByVisibleText("Celcius");
考虑一下我的表格,比如:

<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>

失败
塞尔修斯

有时这种方法行不通。如果选项标记之间的文本在标记前后都有空格,则可能发生这种情况。 例如

1.
失败
2.
失败

在上述示例中,1和2是不同的。 所以你可以用

driver.findElement(By.id("type")).click();     // this will expand the list
driver.findElement(By.xpath("//select[@id='type']/option[contains(text(),'Celcius')]")).click();
也可以直接点击。如果元素是活的,它就会工作

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

driver.findElement(By.id(“type”)).sendkeys(“Celcius”)

也许是因为课文前后的空格?为什么不考虑<代码> StuttBythys>代码> >代码> StastByValue<代码>以提高精度?(没有)我的Selejava代码有些问题……使用接口工作得很好,但是在java单元的情况下,它没有。