selenium webdriver如何操作select下拉框

1、给下来框赋予值(网上转载)

selenium webdriver处理select下拉框,具体例子如下

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class SelectsStudy {
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe"); 
  WebDriver dr = new FirefoxDriver();
  dr.get("http://passport.51.com/reg2.5p");
  
  //通过下拉列表中选项的索引选中第二项,即2011年
  Select selectAge = new Select(dr.findElement(By.id("User_Age")));
  selectAge.selectByIndex(2);
  
  //通过下拉列表中的选项的value属性选中"上海"这一项
  Select selectShen = new Select(dr.findElement(By.id("User_Shen")));
  selectShen.selectByValue("上海");
  
  //通过下拉列表中选项的可见文本选中"浦东"这一项
  Select selectTown = new Select(dr.findElement(By.id("User_Town")));
  selectTown.selectByVisibleText("浦东");
  
  //这里只是想遍历一下下拉列表所有选项,用click进行选中选项
  Select selectCity = new Select(dr.findElement(By.id("User_City")));
  for(WebElement e : selectCity.getOptions())
   e.click();
 }
}
从上面可以看出,对下拉框进行操作时首先要定位到这个下拉框,new 一个Selcet对象,然后对它进行操作。

2、Select 常用到的方法:

方法说明
void deselectAll()取消所有选择项,仅对下拉框的多选模式有效,若下拉不支持多选模式,则会抛出异常 UnsupportedOperationException(不支持的操作)
void deselectByIndex(int index)取消指定index的选择,index从零开始,仅对多选模式有效,否则抛出异常 UnsupportedOperationException(不支持的操作)
void deselectByValue(String value)取消Select标签中,value为指定值的选择,仅对多选模式有效,否则抛出异常 UnsupportedOperationException(不支持的操作)
void deselectByVisibleText(String Text)取消项的文字为指定值的项,例如指定值为Bar,项的html为 <option value="foo">Bar</option>,仅对多选模式有效,单选模式无效,但不会抛出异常
List<WebElement>getAllSelectedOptions() 获得所有选中项,单选多选模式均有效,但没有一个被选中时,返回空列表,不会抛出异常
WebElement getFirstSelectedOption() 获得第一个被选中的项,单选多选模式均有效,当多选模式下,没有一个被选中时,会抛出NoSuchElementException异常
List<WebElement>getOptions() 获得下拉框的所有项,单选多选模式均有效,当下拉框没有任何项时,返回空列表,不会抛出异常
boolean isMultiple() 判断下拉框是否多选模式
void selectByIndex(int index)选中指定index的项,单选多选均有效,当index超出范围时,抛出NoSuchElementException异常
void selectByValue(String value)选中所有Select标签中,value为指定值的所有项,单选多选均有效,当没有适合的项时,抛出NoSuchElementException异常
void selectByVisibleText(String text)选中所有项的文字为指定值的项,与deselectByValue相反,但单选多选模式均有效,当没有适合的项时,抛出NoSuchElementException异常

 

2、selenium  webdriver如何获得select option 选中的值

HTMl:

 

java代码:

实现方法一:

 

List<WebElement> list = dr.findElement(By.id("update_repaymentDateType")).findElements(By.tagName("option"));//获取所有的option元素
System.out.println(list.size());
for(int i=0;i<list.size();i++) {//循环找到被选中的那个option
  if(null!=list.get(i) && "true".equals(list.get(i).getAttribute("selected"))) {//注意:如果把”true“写到equals后面,那么当 list.get(i)获取的元素中无selected属性时则就会报空指针异常
 System.out.println(list.get(i).getText());
	break;
}else {
	continue;
	}
}

 

实现方法二:

Select s=fSelect(dr, By.id("update_repaymentDateType"));
List<WebElement> list = s.getAllSelectedOptions();//获取被选中的options数组
System.out.println(list.get(0).getText());

  

 3、有多个select时,且所有select的属性都相同,如何准确定位到具体的某个select(写这方面的脚本时可以先用selenium IDE录制一套脚本,即可用轻松看出其获取元素的方法)

HTML界面:

java代码:

实现方法一:

 

//新选择一个还款日(当ID和name都不是唯一时,那么就从标签的最外层一层一层的往下层标签定位)
	Select sele=fSelect(dr, By.xpath("(//*[@id='update_repaymentDate'])[2]")); //select的xpath://*[@id="update_repaymentDate"]  
       sele.selectByValue("4");

 说明:以上是通过select的xpath获取的,且在xpath上加上一对” () “和" 【2】 "即可指的就是获取到的是第二个select

            select的 原始xpath://*[@id="update_repaymentDate"]

          获取具体某个select时则把xpath更改为:(//*[@id='update_repaymentDate'])[2]

 实现方法二:获取所有的select然后循环操作

  

List<WebElement> listl = dr.findElement(By.id("repaymentDate_span_select2")).findElements(By.tagName("select"));
int size = dr.findElement(By.id("repaymentDate_span_select2")).findElements(By.tagName("select")).size();
System.out.println(size);
Select sl = new Select(listl.get(1));//获取第二个select
sl.selectByIndex(3);

具体例子如下:

HTML界面:(添加产品时选择两个产品)

java代码:

 //产品构成(添加两个产品)
		WebElement addbutton=fFind(dr, By.id("product_div_01")).findElement(By.tagName("input"));//点击第一个加号按钮
		addbutton.click();
		Select select=null;//创建一个空对象
		int inde=31;//定义一个产品在excel中的第几行
	   List<WebElement>  ww=fFinds(dr, By.name("productIds"));
	   int size=ww.size();
	   int size1=size-1;//程序中当点击+按钮时会新增一个产品下拉列表框,而新的下拉列表框这部分代码直接写死在了程序中,所要减去那个多余的下拉列表框
	   for (int i = 0; i < size1; i++) {
		select=new Select(ww.get(i));
		inde=inde+i;
		select.selectByVisibleText(Demo.getExcel(index, inde, colNum));

  

 

转载于:https://www.cnblogs.com/baixiaozheng/p/4942984.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值