51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1753|回复: 1
打印 上一主题 下一主题

Java+Selenium3自动化入门4---Select多选框下拉列表

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-2-11 13:58:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在做自动化的过程中我们会遇到很多的控件,有的控件在WebDriver中都有封装好的API,我们使用这些方法来操作会提高我们的测试用例编写效率和准确性,今天我就来介绍下关于select多选框的操作方法

在Selenium中,针对html的标签select多选下拉列表有几种方法:
  1. selectByIndex(index);  //根据索引选择
  2. selectByValue(value); //根据value属性选择
  3. selectByVisibleText(text); //根据选项文字选择
  4. 注意的是:
  5. *index是从0开始的
  6. **Value是option标签的一个属性值,并不是显示在下拉框中的值
  7. ***VisibleText是在option标签中间的值,是显示在下拉框的值
复制代码

四种取消方法:
  1. deselectByIndex(0);
  2. deselectByValue(value);
  3. deselectByVisibleText(Text);
  4. deselectAll();       //取消所有选中
复制代码
  1. <!--此部分为HTML代码,供测试时使用-->
  2. <html>
  3.         <head>
  4.                 <title>
  5.                         Selenium for Select  
  6.                 </title>
  7.         </head>
  8.         <body>
  9.                
  10.                 <font size="5" color="red">选择你的兴趣爱好:</font>
  11.                 <!-- multiple=multiple指的是允许多选-->
  12.                 <select name="lions" size=6 multiple=multiple>
  13.                         <option id="basketball" value="basketball">篮球</option>
  14.                         <option id="billiards" value="billiards">台球</option>
  15.                         <option id="table_tennis" value="table_tennis">乒乓球</option>
  16.                         <option id="swimming" value="swimming">游泳</option>
  17.                         <option id="girl" value="girl">撩妹</option>
  18.                         <option id="default" value="默认" selected="selected">--请选择--</option>
  19.                 </select>
  20.         </body>
  21. </html>
复制代码

页面效果如下图:


测试代码:

  1. package com.yumeihu.D1;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.firefox.FirefoxDriver;
  5. import org.openqa.selenium.support.ui.Select;

  6. public class LionsMultipleOptionDropList {
  7. /*
  8. * Today is  very  hot  2018-06-13
  9. */
  10.    
  11.     public static void main(String[] args) throws Exception {   
  12.   
  13.         WebDriver driver = new FirefoxDriver();        
  14.         driver.manage().window().maximize();   
  15.         String Url = "file:///C:/Users/ber.ear/Desktop/lions.htm";
  16.         driver.get(Url);
  17.         //使用 name 属性找到页面上 name 属性为 lions的下拉列表元素
  18.         Select dropList = new Select(driver.findElement(By.name("lions")));
  19.         //使用选择项索引选择"篮球"选项
  20.         dropList.selectByIndex(0);
  21.         //使用 value 属性值选择"游泳"选项
  22.         dropList.selectByValue("swimming");
  23.         //使用选项文字选择"乒乓球"选项
  24.         dropList.selectByVisibleText("乒乓球");
  25.         //等待 3 秒
  26.         Thread.sleep(3000);
  27.         //deselectAll 方法表示取消所有选项的选中状态
  28.         dropList.deselectAll();
  29.         //再次选中 3 个选项
  30.         dropList.selectByIndex(2);
  31.         dropList.selectByValue("table_tennis");
  32.         dropList.selectByVisibleText("篮球");
  33.         //deselectByIndex 方法表示取消索引为 3 的选项的选中状态,因为索引Index是从0开始的
  34.         dropList.deselectByIndex(2);
  35.         
  36.     }
  37. }
复制代码

That's all,通过Select提供的方法和属性,我们可以对标准select下拉框进行任何操作,但是对于非select标签的伪下拉框(即input标签写的假下拉框!!),就需要用其他的方法了,我们下一节说说如何遍历下拉框中的数据。


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-24 10:16 , Processed in 0.065906 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表