zoukankan      html  css  js  c++  java
  • 章节十一、7-操作下拉列表元素

    如下图就是下拉框以及对应的标签属性:

     以下演示操作以图中页面为例(图中的页面是本地的网页,小伙伴们如果需要可以加入555191854或者找其他有下拉的网站进行练习):

    有3种方式可以从下拉列表中选取值:

    a、使用索引来选择

    b、通过value的值来选择

    c、根据文本值来选择

     1 package basicweb;
     2 
     3 import java.util.List;
     4 import java.util.concurrent.TimeUnit;
     5 
     6 import org.junit.jupiter.api.AfterEach;
     7 import org.junit.jupiter.api.BeforeEach;
     8 import org.junit.jupiter.api.Test;
     9 import org.openqa.selenium.By;
    10 import org.openqa.selenium.WebDriver;
    11 import org.openqa.selenium.WebElement;
    12 import org.openqa.selenium.chrome.ChromeDriver;
    13 import org.openqa.selenium.support.ui.Select;
    14 
    15 class DropdownSelect {
    16 
    17     WebDriver driver;
    18     String url;
    19     
    20     @BeforeEach
    21     void setUp() throws Exception {
    22         driver = new ChromeDriver();
    23         url = "C:\Users\acer\eclipse-workspace\SeleniumPractise\PracticePage.html";
    24         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    25         driver.manage().window().maximize();
    26         driver.get(url);
    27     }
    28     
    29     @Test
    30     void test() throws Exception {
    31 //        定位下拉列表
    32         WebElement element = driver.findElement(By.id("fruitselect"));
    33 //        创建对象
    34         Select select = new Select(element);
    35         
    36         Thread.sleep(2000);
    37         System.out.println("选择第二个下拉框元素(桔子)");
    38 //        通过标签的value值来选择
    39         select.selectByValue("orange");
    40 
    41         Thread.sleep(2000);
    42         System.out.println("选择第一个下拉框元素(苹果)");
    43 //        通过下标索引直接选择
    44         select.selectByIndex(0);
    45 
    46         Thread.sleep(2000);
    47         System.out.println("选择第三个下拉框元素(桃子)");
    48 //        通过下拉选项的文本值来选择
    49         select.selectByVisibleText("桃子");
    50         
    51         Thread.sleep(2000);
    52         System.out.println("通过集合打印操作的下拉框元素:");
    53 //        getOptions方法可以返回一个集合,将WebElement中接受到的元素按照下拉列表角标依次返回到el集合中
    54         List<WebElement> el = select.getOptions();
    55         int size = el.size();
    56         for(int i=0; i<size; i++) {
    57 //            用一个String类型的变量来介绍取到的字符串文本
    58             String elText = el.get(i).getText();
    59         System.out.println("索引位为"+i+"的下拉框元素文本值为:"+elText);
    60         }
    61     }
    62 
    63     @AfterEach
    64     void tearDown() throws Exception {
    65         Thread.sleep(2000);
    66         driver.quit();
    67     }
    68 }

    运行结果:

    注意:

    1、下拉列表的值角标从0开始,而非1

    2、引用select对象时,需要导入相应的包,否则eclipse自检代码不通过就会报错

    3、代码中使用的集合也需要导包

     4、Select select = new Select(element)

    select对象只能对select标签的元素进行操作

    如果是ul类型的标签则无效果

    如果有不明白的小伙伴可以加群“555191854”问我,群里都是软件行业的小伙伴相互一起学习。

  • 相关阅读:
    MyBatis高级查询 存储过程
    MyBatis高级查询 一对多映射
    第20章-使用JMX管理Spring Bean
    第19章-使用Spring发送Email
    第18章-使用WebSocket和STOMP实现消息功能
    第17章-Spring消息
    第16章-使用Spring MVC创建REST API
    第15章-使用远程服务
    基于IKAnalyzer搭建分词服务
    第08章-使用Spring Web Flow
  • 原文地址:https://www.cnblogs.com/luohuasheng/p/10839058.html
Copyright © 2011-2022 走看看