您的位置:首页 > 其它

selenium webdriver如何操作select下拉框

2015-11-06 16:31 495 查看
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、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));


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