您的位置:首页 > 其它

selenium获取下拉框中的选中的文本的方式

2016-06-20 21:07 253 查看



示例代码如下:

<select id="ctl00_MainContentPlaceHolder_NoticeManage1_ddlNoticeType" class="select" name="ctl00

$MainContentPlaceHolder$NoticeManage1$ddlNoticeType">

<option value="0">请选择</option>

<option value="8">重要消息</option>

<option value="9">证券要闻</option>

<option value="10">大赛规则</option>

<option value="11">21212</option>

</select>

有多种方式,先定位到选择框的id,然后再取值

//通过下拉列表中选项的索引选中第二项

Select selectAge = new Select(dr.findElement(By.id("你要选择的值id")));

selectAge.selectByIndex(2);

//通过下拉列表中的选项的value属性选中这一项

Select selectShen = new Select(dr.findElement(By.id("你要选择的值id")));

selectShen.selectByValue("value值");

//通过下拉列表中选项的可见文本选中

Select selectTown = new Select(dr.findElement(By.id("你要选择的值id")));

selectTown.selectByVisibleText("文本值");

//通过Xpath定位

Select selectSex = new Select(dr.findElement(By.xpath("//[@id='你要选择的id的值']/option[2]").click(); //表示定位到该id节点下第2个option元素

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: