我使用 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. <option> Fail </option>
2.<option>Fail</option>
上面的例子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();
关于java - Selenium 没有选择正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39270609/