下面我们来看一下selenium webdriver是如何来处理select下拉框的,以http://passport.51.com/reg2.5p这个页面为例。这个页面中有4个下拉框,下面演示4种选中下拉框选项的方法。select处理比较简单,直接看代码吧:)

  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.firefox.FirefoxDriver;
  5. import org.openqa.selenium.support.ui.Select;
  6. public class SelectsStudy {
  7. /**
  8. * @author gongjf
  9. */
  10. public static void main(String[] args) {
  11. // TODO Auto-generated method stub
  12. System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
  13. WebDriver dr = new FirefoxDriver();
  14. dr.get("http://passport.51.com/reg2.5p");
  15. //通过下拉列表中选项的索引选中第二项,即2011年
  16. Select selectAge = new Select(dr.findElement(By.id("User_Age")));
  17. selectAge.selectByIndex(2);
  18. //通过下拉列表中的选项的value属性选中"上海"这一项
  19. Select selectShen = new Select(dr.findElement(By.id("User_Shen")));
  20. selectShen.selectByValue("上海");
  21. //通过下拉列表中选项的可见文本选 中"浦东"这一项
  22. Select selectTown = new Select(dr.findElement(By.id("User_Town")));
  23. selectTown.selectByVisibleText("浦东");
  24. //这里只是想遍历一下下拉列表所有选项,用click进行选中选项
  25. Select selectCity = new Select(dr.findElement(By.id("User_City")));
  26. for(WebElement e : selectCity.getOptions())
  27. e.click();
  28. }
  29. }

从上面可以看出,对下拉框进行操作时首先要定位到这个下拉框,new 一个Selcet对象,然后对它进行操作。

selenium webdriver学习(八)------------如何操作select下拉框(转)的更多相关文章

  1. Selenium常用操作汇总二——如何操作select下拉框

    下面我们来看一下selenium webdriver是如何来处理select下拉框的,以http://passport.51.com/reg2.5p这个页面为例.这个页面中有4个下拉框,下面演示4种选 ...

  2. jquery操作select下拉框的各种方法,获取选中项的值或文本,根据指定的值或文本选中select的option项等

    简介jquery里对select进行各种操作的方法,如联动.取值.根据值或文本来选中指定的select下拉框指定的option选项,读取select选中项的值和文本等. 这一章,站长总结一下jquer ...

  3. JQuery操作select下拉框

    JQuery操作select下拉框 获取Select选择的Text和Value $("#select_id").change(function(){//code...}); //为 ...

  4. Selenium系列(十) - 针对Select下拉框的操作和源码解读

    如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...

  5. Selenium2学习(十一)-- select下拉框

    本篇以百度设置下拉选项框为案例,详细介绍select下拉框相关的操作方法. 一.认识select    1.打开百度-设置-搜索设置界面,如下图所示 2.箭头所指位置,就是select选项框,打开页面 ...

  6. jquery操作select下拉框的多种方法(选中,取值,赋值等)

    Query获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...}); //为Sel ...

  7. jQuery操作select下拉框的text值和value值的方法

    1.jquery获取当前选中select的text值 $("#select1").find("option:selected").text(); 2.jquer ...

  8. jquery操作select下拉框:取值,赋值,删除

    1.jQuery对select的取值 <select id="test"> <option value ="1">测试1</opt ...

  9. Python+Selenium操作select下拉框

    首先需要倒入Select模块: from selenium.webdriver.support.select import Select 常用方法: 通过索引定位:select_by_index() ...

随机推荐

  1. python 轴向连接

  2. golang标准库中有些函数只有签名没有函数体是怎么回事?

  3. Ionic.Zip

    1.Ionic.zIP 实现文件压缩和解压 2.压缩: /// <summary>        /// 压缩文件        /// </summary>        / ...

  4. BZOJ1455罗马游戏

    左偏树裸题. 题面描述让人意识到了平面几何的重要性. //Achen #include<algorithm> #include<iostream> #include<cs ...

  5. selenium 常见问题之 nknown error: call function result missing ‘value’

    运行时候出现错误提示如下: 出现该问题原因:chrome浏览器自动升级.导致和chromedriver支持的版本不匹配. 解决方案有两种(本人采用的是第一种方式解决办法.): 1.下载和当前使用的ch ...

  6. hdu 1711Number Sequence (KMP入门,子串第一次出现的位置)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. 计算机组成原理作业一 熟悉MIPS指令

    第一题 .data outputd: .asciiz "Alpha","November","First","alpha" ...

  8. tablespaces

    select * from user_tablespaces; select username,default_tablespace from user_users;

  9. POJ1190 洛谷P1731 NOI1999 生日蛋糕

    生日蛋糕(蛋糕是谁?) Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20272   Accepted: 7219 Desc ...

  10. PHPCMS快速建站系列之getcache()的用法

    /** * 读取缓存,默认为文件缓存,不加载缓存配置. * @param string $name 缓存名称 * @param $filepath 数据路径(模块名称) caches/cache_$f ...