下面我们来看一下selenium webdriver是如何来处理select下拉框的,以Apple注册页面为例。

https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/createAppleId

  1. package com.annie.test;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import org.openqa.selenium.By;
  6. import org.openqa.selenium.WebDriver;
  7. import org.openqa.selenium.WebElement;
  8. import org.openqa.selenium.firefox.FirefoxDriver;
  9. import org.openqa.selenium.support.ui.Select;
  10. import static org.junit.Assert.*;
  11. import org.junit.Test;
  12. public class SelectTest {
  13. @Test
  14. public void testDropdown() {
  15. // System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
  16. WebDriver dr = new FirefoxDriver();
  17. dr
  18. .get("https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/createAppleId");
  19. // 通过下拉列表中选项的索引选中第二项,
  20. // 即What is the first name of your best friend in high school?
  21. Select sq1 = new Select(dr.findElement(By.id("security-question_1")));
  22. sq1.selectByIndex(2);
  23. // 通过下拉列表中的选项的value属性选中"January"value=1 这一项
  24. Select selectMon = new Select(dr.findElement(By.id("month")));
  25. selectMon.selectByValue("1");
  26. assertFalse(selectMon.isMultiple());// 验证下拉列表的不支持多选
  27. // assertEquals(4,selectMon().size()); //验证下拉数量
  28. Select selectDay = new Select(dr.findElement(By.id("day")));
  29. selectDay.selectByVisibleText("23");
  30. /** 检查下拉列表的选项 */
  31. // 预期的选项内容StateOptions
  32. List<String> StateOptions = Arrays.asList(new String[] {
  33. "Please select", "Alabama", "Alaska", "Arizona", "Arkansas",
  34. "Armed Forces Americas", "Armed Forces Europe",
  35. "Armed Forces Pacific", "California", "Colorado",
  36. "Connecticut", "Delaware", "Dist of Columbia", "Florida",
  37. "Georgia", "Guam", "Hawaii", "Idaho", "Illinois", "Indiana",
  38. "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
  39. "Massachusetts", "Michigan", "Minnesota", "Mississippi",
  40. "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
  41. "New Jersey", "New Mexico", "New York", "North Carolina",
  42. "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania",
  43. "Puerto Rico", "Rhode Island", "South Carolina",
  44. "South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
  45. "Virgin Islands", "Virginia", "Washington", "West Virginia",
  46. "Wisconsin", "Wyoming" });
  47. /** 遍历一下下拉列表所有选项,用click进行选中选项 **/
  48. Select selectState = new Select(dr.findElement(By.id("state-province")));
  49. List<String> act_StateOptions = new ArrayList<String>();
  50. // 判断选择内容
  51. assertEquals("Please select", selectState.getFirstSelectedOption()
  52. .getText());
  53. for (WebElement e : selectState.getOptions()) {
  54. e.click();
  55. //  s = s + "\"" + e.getText() + "\"" + ",";
  56. // 将实际的下拉列表内容注入到act_StateOptions中,进行比较。
  57. act_StateOptions.add(e.getText());
  58. }
  59. assertArrayEquals(StateOptions.toArray(), act_StateOptions.toArray());
  60. }
  61. }
  1. package com.annie.test;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import org.openqa.selenium.By;
  6. import org.openqa.selenium.WebDriver;
  7. import org.openqa.selenium.WebElement;
  8. import org.openqa.selenium.firefox.FirefoxDriver;
  9. import org.openqa.selenium.support.ui.Select;
  10. import static org.junit.Assert.*;
  11. import org.junit.Test;
  12. public class SelectTest {
  13. @Test
  14. public void testDropdown() {
  15. // System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
  16. WebDriver dr = new FirefoxDriver();
  17. dr
  18. .get("https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/createAppleId");
  19. // 通过下拉列表中选项的索引选中第二项,
  20. // 即What is the first name of your best friend in high school?
  21. Select sq1 = new Select(dr.findElement(By.id("security-question_1")));
  22. sq1.selectByIndex(2);
  23. // 通过下拉列表中的选项的value属性选中"January"value=1 这一项
  24. Select selectMon = new Select(dr.findElement(By.id("month")));
  25. selectMon.selectByValue("1");
  26. assertFalse(selectMon.isMultiple());// 验证下拉列表的不支持多选
  27. // assertEquals(4,selectMon().size()); //验证下拉数量
  28. Select selectDay = new Select(dr.findElement(By.id("day")));
  29. selectDay.selectByVisibleText("23");
  30. /** 检查下拉列表的选项 */
  31. // 预期的选项内容StateOptions
  32. List<String> StateOptions = Arrays.asList(new String[] {
  33. "Please select", "Alabama", "Alaska", "Arizona", "Arkansas",
  34. "Armed Forces Americas", "Armed Forces Europe",
  35. "Armed Forces Pacific", "California", "Colorado",
  36. "Connecticut", "Delaware", "Dist of Columbia", "Florida",
  37. "Georgia", "Guam", "Hawaii", "Idaho", "Illinois", "Indiana",
  38. "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
  39. "Massachusetts", "Michigan", "Minnesota", "Mississippi",
  40. "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
  41. "New Jersey", "New Mexico", "New York", "North Carolina",
  42. "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania",
  43. "Puerto Rico", "Rhode Island", "South Carolina",
  44. "South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
  45. "Virgin Islands", "Virginia", "Washington", "West Virginia",
  46. "Wisconsin", "Wyoming" });
  47. /** 遍历一下下拉列表所有选项,用click进行选中选项 **/
  48. Select selectState = new Select(dr.findElement(By.id("state-province")));
  49. List<String> act_StateOptions = new ArrayList<String>();
  50. // 判断选择内容
  51. assertEquals("Please select", selectState.getFirstSelectedOption()
  52. .getText());
  53. for (WebElement e : selectState.getOptions()) {
  54. e.click();
  55. //  s = s + "\"" + e.getText() + "\"" + ",";
  56. // 将实际的下拉列表内容注入到act_StateOptions中,进行比较。
  57. act_StateOptions.add(e.getText());
  58. }
  59. assertArrayEquals(StateOptions.toArray(), act_StateOptions.toArray());
  60. }
  61. }

/**从上面可以看出,对下拉框进行操作时首先要定位到这个下拉框,new 一个Selcet对象,然后对它进行操作。 在执行上面的例子时需要导入
* org.openqa
* .selenium.support.ui.Select类。首先创建一个Select癿对象,isMultiple()用来判断是丌是多选下拉框
* 。Select类提供了3种方法来选择下拉选项
* 。selectByVisibleText(),selectByValue(),selectByIndex()。
* 在使用返些方法癿时候要注意下拉列表是否是动态变化的 。
*/

如果只是单选的下拉列表,通过 如果只是单选的下拉列表,通过 getFirstSelectedOption()就可以得到所选择的项, 再调 用 getText() 就可以得到本文。 如果是多选的下拉列表,使用 getAllSelectedOptions() 得到所有已选择的项,此方法 会返回元素的集合。 使用  assertArrayEquals()方法来对比期望和实际所选的项是否正确。 调用 getAllSelectedOptions().size()方法来判断已选的下拉列表项数量。 如果想检查 某一个选项是否被择了,可以使用 assertTrue(act_sel_options.contains(“Red”)) 方 法

运行结果

select 自动选择 检查下拉列表的更多相关文章

  1. js单击自动选择文本

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  2. Android小项目之四 自动更新检查的逻辑

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 按惯例,写在前面的:可能在学习Android的过程中,大家会和我一样,学习过大量的基础知识,很多的知识点 ...

  3. ECshop网点程序优化-后台添加类目自动选择上次父类目并计算Sort Order

    如果在ECshop后台批量添加过大量类目的人都能体会到是多么的不方便(这点还是要说一下ECshop的产品经理,细节上还是要多注意),每次添加都需要在几百个类目里面找到要添加的父类目也是一个麻烦事,比如 ...

  4. JS-加载页面的时候自动选择刚才所选择option

    <body class="no-skin" onload="option_auto(${pd.PACK_SORT})"> <select na ...

  5. 自动选择最佳特征进行分类-SVM (Halcon)

    HALCON12里的example,classify_pills_auto_select_features.hdev. 执行流程: 1.选取相关特征(本例选取color和region组的所有特征)(本 ...

  6. 【摘录】JAVA内存管理-自动选择垃圾收集器算法

    在J2SE 5.0,垃圾收集的默认值:垃圾收集器.堆大小以及JVM的类型(客户端还是服务器)都会根据应用运行的硬件平台和操作系统自动选择.相比之前设置命令行参数的方式,自动选择很好的匹配了不同类型的应 ...

  7. js进阶 9-10 html控件如何实现点击自动选择控件内容

    js进阶 9-10  html控件如何实现点击自动选择控件内容 一.总结 一句话总结: 1.在click事件中,如果focus,那就select 2.blur 1.html中控件添加两种方式? 在表单 ...

  8. Uipath 选择页面下拉列表中的选项

    http://www.rpatokyo.com/ 使用Select item Activity选择页面下拉列表中的选项 在open browser中拖入Select Item Activity,配置参 ...

  9. XML:使用DOM技术解析xML文件中的城市,实现select级联选择

    中国的城市xml格式:cities.xml <?xml version="1.0" encoding="utf-8"?> <china> ...

随机推荐

  1. TensorFlow基础笔记(4) Tensor Transformation

    https://segmentfault.com/a/1190000008793389 抽取 tf.slice(input_, begin, size, name=None):按照指定的下标范围抽取连 ...

  2. Applet是java的自动执行方式(这是它的优势,主要用于HTML)

    进度条:ProgressBar. JcomboBox:下拉菜单:在AWT中同类组件是choice. JlistPanel:选择列表 BorderPanel:设置边框 JsplitPanel:可将容器分 ...

  3. 【BZOJ】1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场(dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1619 首先不得不说,,题目没看懂.... 原来就是找一个下降的联通块.... 排序后dfs标记即可. ...

  4. 书籍:Building Secure PHP Apps

    Building Secure PHP Apps https://leanpub.com/buildingsecurephpapps

  5. 去掉if

    修改前 namespace CleanCSharp.Methods.Dirty {     class BooleanSwitchingArgumentsExample     {         p ...

  6. 如何编写jQuery插件

    要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...

  7. ETL概念,ETL流程

    ETL是将业务系统的数据经过抽取.清洗转换之后加载到数据仓库的过程,目的是将企业中的分散.零乱.标准不统一的数据整合到一起,为企业的决策提供分析依据. ETL是BI项目重要的一个环节. 通常情况下,在 ...

  8. 【BZOJ4071】[Apio2015]巴邻旁之桥 Treap

    [BZOJ4071][Apio2015]巴邻旁之桥 Description 一条东西走向的穆西河将巴邻旁市一分为二,分割成了区域 A 和区域 B. 每一块区域沿着河岸都建了恰好 1000000001 ...

  9. Hibernate如何执行存储过程?

    Hibernate如何执行存储过程? @Overridepublic Boolean setVarValue(final String processInstanceId, final String ...

  10. Java中对List集合的常用操作(转载)

    目录: list中添加,获取,删除元素: list中是否包含某个元素: list中根据索引将元素数值改变(替换): list中查看(判断)元素的索引: 根据元素索引位置进行的判断: 利用list中索引 ...