微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Selenium操作详解之下拉列表操作

操作单选下拉列表——打印每一个选项——3种方法选中某一个选项——select类



public static void main(String[] args) {

// Todo Auto-generated method stub

System.setProperty("webdriver.chrome.driver", ".\\target\\chromedriver.exe");

// 声明ChromeOptions类的对象option  

ChromeDriver driver = new ChromeDriver();   //初始化谷歌浏览器实例,并打开浏览器

try {

driver.get("http://www.baidu.com"); //打开一个网址

driver.manage().window().maximize();  //最大化窗口

Thread.sleep(1000);

Actions action = new Actions(driver);

action.movetoElement(driver.findElement(By.linkText("设置"))).perform();//鼠标悬浮在设置元素上面

driver.findElement(By.linkText("搜索设置")).click();//打开搜索设置

Thread.sleep(1000);

Select dropList = new Select(driver.findElementByName("NR"));

Thread.sleep(1000);

System.out.println(dropList.getFirstSelectedOption().getText());//查看刚开始认选择的选项

Boolean a = dropList.isMultiple();//是否允许多选,允许多选返回true,否则不允许返回false

System.out.println(a);//此处不允许多选,返回false

Thread.sleep(1000);

 System.out.println("-------------------------------");

 List<WebElement> options = dropList.getoptions();

         for (WebElement option : options )

         {

             System.out.println(option.getText());

         }

         System.out.println("-------------------------------");

       //------------------------------------------------------------------- 

    //selectByIndex()方法,通过索引进行选中,3个选项,索引从0开始,即   0,1,2

         

      dropList.selectByIndex(1);         //3个选项,索引从0开始,即   0,1,2

         System.out.println(dropList.getFirstSelectedOption().getText());

         Thread.sleep(5000);

         dropList.selectByIndex(2);

         System.out.println(dropList.getFirstSelectedOption().getText());

         Thread.sleep(5000);

         dropList.selectByIndex(0);

         System.out.println(dropList.getFirstSelectedOption().getText());

         Thread.sleep(5000);

       //------------------------------------------------------------------         System.out.println("-------------------------------");

         //selectByVisibleText()方法,通过选项的文字进行选中

         dropList.selectByVisibleText("每页显示20条");

         System.out.println(dropList.getFirstSelectedOption().getText());

         Thread.sleep(5000);

         dropList.selectByVisibleText("每页显示50条");

         System.out.println(dropList.getFirstSelectedOption().getText());

         Thread.sleep(5000);

         dropList.selectByVisibleText("每页显示10条");

         System.out.println(dropList.getFirstSelectedOption().getText());

         Thread.sleep(5000);

       //---------------------------------------------------------------

         System.out.println("-------------------------------");      

         //selectByValue()方法,使用下来列表的属性的value属性值进行选中操作      

         dropList.selectByValue("20");

         System.out.println(dropList.getFirstSelectedOption().getText());

         Thread.sleep(5000);

         dropList.selectByValue("50");

         System.out.println(dropList.getFirstSelectedOption().getText());

         Thread.sleep(5000);

         dropList.selectByValue("10");

         System.out.println(dropList.getFirstSelectedOption().getText());

         Thread.sleep(5000);

         Thread.sleep(10000);

    }catch (Exception e)

    {

        e.printstacktrace();

    }finally 

    {

        driver.quit();

}

}

}

 

上面是我收集的一些视频资源包

对于软件测试的的朋友来说应该是最全面最完整的备战仓库了,为了更好地整理每个模块,我也参考了很多网上的优质博文和项目,力求不漏掉每一个知识点,很多朋友靠着这些内容进行复习,拿到了BATJ等大厂的offer,这个仓库也已经帮助了很多的软件测试的学习者,希望也能帮助到你!

关注我的微信公众号【程序员二黑】免费获取

如果你对软件测试、接口测试、自动化测试、面试经验交流感兴趣欢迎加入:软件测试技术群:785128166 里面有大牛分享学习经验
 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐