zoukankan      html  css  js  c++  java
  • 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));
    

      

  • 相关阅读:
    Form表单中不同的按钮进行不同的跳转
    Redis查询&JDBC查询&Hibernate查询方式的效率比较...
    JDBC批处理读取指定Excel中数据到Mysql关系型数据库
    使用JDBC-ODBC读取Excel文件
    Linux公社资料库地址
    用Shell实现俄罗斯方块代码(Tetris.sh)
    Storm累计求和中使用各种分组Grouping
    Storm累计求和Demo并且在集群上运行
    CSS中margin和padding的区别
    使用json-lib-*.jar的JSON解析工具类
  • 原文地址:https://www.cnblogs.com/baixiaozheng/p/4942984.html
Copyright © 2011-2022 走看看