select下拉框处理

发布时间 2023-04-08 21:47:07作者: TestRookie

1. selectByIndex() 根据索引来选取,从0开始

2. selectByValue() 根据属性value的属性值来选取

3. selectByVisibleText()根据标签之间的Text值,也就是页面显示的

注意:导入的包要正确不然会报错。   import org.openqa.selenium.support.ui.Select;

单选下拉列表: 

HTML源码

<tr>
    <td>Select下拉列表</td>
          <div id='select'>
                <select id="selected">
                    <option value ="a">huawei</option>
                    <option value ="b">oppo</option>
                    <option value="c">vivo</option>
                    <option value="d">xiaomi</option>
                    <option value="e">hongmi</option>
                    <option value="f">meizu</option>
                </select>
          </div>                                
    </tr>

java代码

package cn.WebDriverAPI;
//单选下拉框
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;


public class downList {
    public WebDriver driver;
    @Test
    public void test(){
        System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");
        driver=new ChromeDriver();
        driver.get("file:///C:/Users/Administrator.USER-20180602NR/Desktop/selenium.html");
        WebElement s=driver.findElement(By.xpath("//select[@id='selected']"));
        Select select=new Select(s);
        //isMultiple表示此下拉框列表是否允许多选,返回结果应为false
        Assert.assertFalse(select.isMultiple());
        //输出isMultiple方法返回的值
        Boolean b=select.isMultiple();
        System.out.println("b:"+b);
        //通过选项的文字进行选中
        select.selectByVisibleText("oppo");
        //获取当前被选中的下拉列表选项文本并赋给text1
        String text1=select.getFirstSelectedOption().getText();
        //断言
        Assert.assertEquals("oppo", text1);
        System.out.println("文本"+text1);
        //使用下拉列表选项的value属性值进行选中操作
        select.selectByValue("c");
        //获取当前被选中的下拉列表选项文本并赋给text2
        String text2=select.getFirstSelectedOption().getText();
        //断言
        Assert.assertEquals("vivo",text2);
        //通过索引选择,从0开始
        select.selectByIndex(3);
        String text3=select.getFirstSelectedOption().getText();
        Assert.assertEquals("xiaomi",text3);
    }
    @AfterMethod
    public void afterMethod(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        driver.quit();
    }

}

多选的下拉列表:

HTML源码

<select id="selects" multiple="multiple">
            <option value="java">java</option>
            <option value="c">c</option>
            <option value="c++">c++</option>
            <option value="VB">VB</option>
            <option value="php">php</option>
            <option value="python">python</option>
            <option value="ruby">ruby</option>
</select>

java代码

WebElement mus=driver.findElement(By.id("selects"));
        Select select=new Select(mus);
        //断言下拉列表是否支持多选,支持多选isMultiple方法则返回True
        Assert.assertTrue(select.isMultiple());
        //通过索引选择
        select.selectByIndex(0);
        //获取当前被选中选项的文字
        String text=select.getFirstSelectedOption().getText();
        //断言获取到的文字是否符合实际
        Assert.assertEquals("java",text);
        //通过value值选择
        select.selectByValue("c");
        //通过选项文字选择
        select.selectByVisibleText("VB");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //取消所有选项的选中状态
        select.deselectAll();