Select多选框下拉列表


Select多选框下拉列表

??在做自动化的过程中,我们经常会遇到select标签类型的多选框下拉列表。针对select标签类型,selenium中有封装好的API可以使用,接下来,我们一起看看关于select多选框下拉列表的操作方法吧。

操作方法简介

1、选择下拉列表方法

selectByIndex(index);  		// 根据索引选择
selectByValue(value); 		// 根据value属性选择
selectByVisibleText(text); 	// 根据选项文本值选择
注意:
index是从0开始的
Value是option标签的一个属性值,并不是显示在下拉框中的值
VisibleText是在option标签中间的值,是显示在下拉框的值  

2、取消方法

deselectByIndex(index);			// 根据索引取消
deselectByValue(value);			// 根据value属性取消
deselectByVisibleText(Text);		// 根据选项文本值取消
deselectAll();       			// 取消所有选中

3、其他方法

getAllSelectedOptions();	// 获取所有被选中的标签元素对象集合
getFirstSelectedOption();	// 获取第一个被选中的标签元素对象
select.getOptions();		// 获取所有选项的标签元素对象集合
getWrappedElement();		// 获取包裹具体选项的元素对象
isMultiple();			// 判断该元素是否支持多个对下个,根据mutiple属性来判断,返回布尔值

被测HTML对象

测试用的丐版html:




    


    用户名: 
密 码:
爱好: 篮球电影
性别:
头像:
住址:
博客: 点击进入
备注信息:
姓名年龄分数
张三28100
李四29100
王二29100
麻子29100

页面展示:



代码操作

1、选择下拉列表

@Test
public void testDemo1() throws InterruptedException {
    WebDriver driver = new ChromeDriver();
    driver.get("E:\\Notes\\Selenium\\select.html");
    driver.manage().window().maximize();
    Thread.sleep(3000);
    WebElement element = driver.findElement(By.id("select_province"));
    Select select = new Select(element);
    // 选择索引值为2,即第三个选项
    select.selectByIndex(2);
    Thread.sleep(5000);
    // 选择value属性值为1的选择
    select.selectByValue("1");
    Thread.sleep(5000);
    // 选择文本值为安徽省的选项
    select.selectByVisibleText("安徽省");
    Thread.sleep(5000);
}

2、取消方法

取消方法针对的select标签是需要有multiple属性的,没有则抛异常。

在上面html中的select标签中增加multiple="multiple"

@Test
public void testDemo2() throws InterruptedException {
    WebDriver driver = new ChromeDriver();
    driver.get("E:\\Notes\\Selenium\\select_mutiple.html");
    driver.manage().window().maximize();
    Thread.sleep(3000);
    WebElement element = driver.findElement(By.id("select_city"));
    Select select = new Select(element);
    // 索引值为2的选择后取消
    select.selectByIndex(2);
    Thread.sleep(3000);
    select.deselectByIndex(2);
    Thread.sleep(3000);
    // value属性值为1的选择后取消
    select.selectByValue("1");
    Thread.sleep(3000);
    select.deselectByValue("1");
    Thread.sleep(3000);
    // 选择文本值为安徽省的选项,然后取消
    select.selectByVisibleText("滁州市");
    Thread.sleep(3000);
    select.deselectByVisibleText("滁州市");
    // 取消所有
    select.deselectAll();
    Thread.sleep(3000);
}

3、其他方法

@Test
public void testDemo() throws InterruptedException {
    WebDriver driver = new ChromeDriver();
    driver.get("E:\\Notes\\Selenium\\select.html");
    driver.manage().window().maximize();
    Thread.sleep(3000);
    WebElement element = driver.findElement(By.id("select_province"));
    Select select = new Select(element);

    boolean multiple = select.isMultiple();
    System.out.println("multiple is " + multiple);
    System.out.println("================================================");

    WebElement wrappedElement = select.getWrappedElement();
    System.out.println("wrappedElement is " + wrappedElement.getText());
    System.out.println("================================================");

    List options = select.getOptions();
    System.out.println("The size of allSelectedOptions is " + options.size());
    for(int i=0;i allSelectedOptions = select.getAllSelectedOptions();
    System.out.println("The size of allSelectedOptions is " + allSelectedOptions.size());
    for(int i=0;i

打印结果:

com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.test.day02.SelectTest,testDemo
Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}) on port 37534
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1650212642.945][WARNING]: This version of ChromeDriver has not been tested with Chrome version 97.
四月 18, 2022 12:24:02 上午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: W3C
multiple is false
================================================
wrappedElement is    安徽省
   江苏省
   广东省
  
================================================
The size of allSelectedOptions is 3
第0次:安徽省
第1次:江苏省
第2次:广东省
================================================
firstSelectedOption is 安徽省
================================================
The size of allSelectedOptions is 1
第0次:安徽省

Process finished with exit code 0