java - 无法从硒的下拉列表中选择选项
问题描述
我是 selenium 的新手,我发现很难从下拉列表中获取选项,如果班级之间有空格,我也很难获取 className,请帮助我,提前谢谢。
我将 select 与 selectbyindex 一起使用,也使用了 partialvisibletext,但没有任何效果。
这是 HTML 代码
<div class="form-group" xpath="1">
<label>Source of Info</label>
<!-- <select class="form-control flat-control-inner" ng-model="userPersonal.sourceOfInfo.id" required>
<option ng-value="0">--Select--</option>
<option data-ng-repeat="si in sourceOfInfoList" data-ng-value="{{si.id}}" ng-selected="si.id==userPersonal.sourceOfInfo.id">{{si.name}}</option>
</select> -->
<select class="form-control flat-control-inner dateonly ng-valid ng-touched ng-empty ng-dirty ng-valid-parse" ng-model="userPersonal.sourceOfInfo.id" ng-options="si.id as si.name for si in sourceOfInfoList" style="">
<option value="" class="">--Select--</option>
<option label="Newspaper" value="number:1">Newspaper</option>
<option label="Facebook" value="number:2" selected="selected">Facebook</option>
<option label="Twitter" value="number:3">Twitter</option>
<option label="Television" value="number:4">Television</option>
<option label="Others" value="number:5">Others</option>
</select>
</div>
这是我尝试过的
WebElement source_dropdown = driver.findElement(By.xpath("//select[@class='form-control flat-control-inner dateonly ng-valid ng-touched ng-empty ng-dirty ng-valid-parse']"));
Select source = new Select(source_dropdown);
source.selectByIndex(1);
这就是我在控制台中得到的
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//select[@class='form-control flat-control-inner dateonly ng-valid ng-touched ng-empty ng-dirty ng-valid-parse']"}
解决方案
试试这个来选择与参数匹配的显示文本的选项
//xpath = select[@ng-model='userPersonal.sourceOfInfo.id']
WebElement dropDownListBox = driver.findElement(By.xpath("//select[@ng-model='userPersonal.sourceOfInfo.id']"));
Select clickThis = new Select(dropDownListBox);
clickThis.selectByVisibleText(value);//value is an option visible in dropdown
推荐阅读
- google-apps-script - Google App Script - 不使用 UrlFetchApp 发送带有 .xlsx 附件的电子邮件
- twitter-bootstrap - Jade / Pug 模板中带空格的类
- python - 从 OpenCV python 读取图像后提供图像后键入错误
- vue.js - 将 vuetify 从 1.5 升级到 2.1 时出错
- java - java.lang.NullPointerException 在模拟器和手机中停止我的应用程序
- java - 在 Java 中清除和初始化对象 - Android Studio
- mapbox - Mapbox 自动翻译街道名称
- java - java中的ruby块相当于什么
- amazon-web-services - 如何使用不同的 --endpoint-url 同步 S3 存储桶
- xml - 获取 XPATH 中属性的字符串值