【发布时间】:2019-12-11 17:16:51
【问题描述】:
用例是在 selenium 的帮助下从下拉列表中选择一个项目。我尝试了以下但无法实现我的目标。输入标签是指一个 datalist 。
String baseUrl = "https://angular-cwmwke.stackblitz.io" ;
driver.get(baseUrl); // opens the webpage
// Put some wait for the page to load the dropdown
driver.findElement(By.xpath("//input[@list='id-car']")).click(); // clicks on the dropdown
driver.findElement(By.xpath("//*[@id='id-car']/option[1]")).click(); // Does not works.
它失败了,这是个例外。 JavascriptException: javascript 错误:选项元素不在选择中
Selenium 版本:3.14.0 和 Chrome 版本:78.0.3904.108
这是 HTML(请忽略选项值):
<input _ngcontent-c0="" list="id-car" ng-reflect-model="[object Object]" class="ng-pristine ng-valid ng-touched">
<datalist _ngcontent-c0="" id="id-car">
<!--bindings={
"ng-reflect-ng-for-of": "[object Object],[object Object"
}-->
<option _ngcontent-c0="" value="[object Object]" ng-reflect-value="[object Object]">Ford-GTX</option>
<option _ngcontent-c0="" value="[object Object]" ng-reflect-value="[object Object]">Ferarri - Enzo</option>
<option _ngcontent-c0="" value="[object Object]" ng-reflect-value="[object Object]">VW - Amarok</option>
</datalist>
【问题讨论】:
-
如果下拉菜单上的过渡/动画效果有时会影响选项单击,您可以尝试添加
ExpectedConditions.elementToBeClickable或在选项单击前等待一两秒? -
我确实尝试过等待,但没有帮助。
标签: java html angular selenium html-datalist