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

示例代码如下:

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值