如何从下拉列表中选择值 Selenium
⚡ 智能摘要
从下拉列表中选择值 Selenium 使用专用的 Select 类来控制 HTML SELECT 元素。该类公开了 selectByVisibleText、selectByValue 和 selectByIndex 方法,可以精确地选择选项并支持多选下拉列表。

什么是 Select 类 Selenium?
此 选择课程 Selenium 是来自 org.openqa.selenium.support.ui 允许 WebDriver 与 HTML 交互的软件包 <select> 它提供了按可见文本、值或索引选择和取消选择选项的方法,以及检测下拉列表是否接受多项选择的方法。
计划 Select 是常规的 Java 类,您可以使用以下方式创建实例: new 关键字并传递指向 WebElement 的 WebElement <select> 标签。
如何选择下拉菜单 Selenium
在处理下拉菜单之前 Selenium需要两个步骤:
- 导入包 org.openqa.selenium.support.ui.Select.
- 将下拉菜单实例化为
Select对象在 Selenium WebDriver。
例如,打开 Mercury 旅游注册页面 (https://demo.guru99.com/test/newtours/register.php)并找到“国家/地区”下拉菜单。
步骤 1)导入 Select 包。
import org.openqa.selenium.support.ui.Select;
步骤2)将下拉元素声明为Select类的实例。 在下面的示例中,实例名为 drpCountry.
Select drpCountry = new Select(driver.findElement(By.name("country")));
步骤 3)开始控制下拉菜单。 使用任意选择方法选择一个选项。以下示例选择“南极洲”:
drpCountry.selectByVisibleText("ANTARCTICA");
选择方法 Selenium
Select 类提供了多种与下拉选项交互的方法。下面介绍其中最常用的五种方法。
1) selectByVisibleText() 和 deselectByVisibleText()
- 选中或取消选中显示文本与参数匹配的选项。
- 参数: 选项的准确显示文本。
计费示例:
drpCountry.selectByVisibleText("ANTARCTICA");
2) selectByValue() 和 deselectByValue()
- 选择或取消选择该选项
value属性与参数匹配。 - 请注意显示的文本和
value属性通常各不相同,如下所示。 - 参数: 该选项的
value属性。
计费示例:
drpCountry.selectByValue("234");
3) selectByIndex() 和 deselectByIndex()
- 选中或取消选中指定索引处的选项。索引从零开始。
- 参数: 选项的整数索引。
计费示例:
drpCountry.selectByIndex(0);
4) isMultiple()
- Returns & Exchanges
TRUE如果下拉菜单允许同时选择多个选项,否则FALSE. - 参数: 没有。
计费示例:
if (drpCountry.isMultiple()) {
// do something here
}
5) 取消选择所有()
- 清除所有已选选项。仅适用于多选下拉菜单。
- 参数: 没有。
计费示例:
drpCountry.deselectAll();
完成: Code 选择方法 Selenium
下列 Java 该程序演示了单选和多选下拉列表中的 Select 类。
package newpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
public class accessDropDown {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
String baseURL = "https://demo.guru99.com/test/newtours/register.php";
WebDriver driver = new FirefoxDriver();
driver.get(baseURL);
// Single-select drop-down
Select drpCountry = new Select(driver.findElement(By.name("country")));
drpCountry.selectByVisibleText("ANTARCTICA");
// Multi-select drop-down
driver.get("http://jsbin.com/osebed/2");
Select fruits = new Select(driver.findElement(By.id("fruits")));
fruits.selectByVisibleText("Banana");
fruits.selectByIndex(1);
}
}
在多选下拉列表中选择项目
此 selectByVisibleText() 该方法也适用于多选下拉列表。例如,使用 https://jsbin.com/osebed/2 作为碱 URL它会显示一个下拉菜单,允许一次选择多个选项。
以下代码片段使用以下方式选择前两个选项 selectByVisibleText():
方法参考表
下表总结了上面提到的所有 Select 类方法,以便快速查找。
| 命令 | 描述 |
|---|---|
| selectByVisibleText() / deselectByVisibleText() | 通过显示的文本选择或取消选择选项。 |
| selectByValue() / deselectByValue() | 通过其选择或取消选择某个选项 value 属性。 |
| selectByIndex() / deselectByIndex() | 根据从零开始的索引选择或取消选择选项。 |
| 是多个() | 如果下拉菜单允许多选,则返回 TRUE,否则返回 FALSE。 |
| 取消全部选择() | 取消选择多选下拉菜单中所有先前已选择的选项。 |
要控制下拉框,请导入 org.openqa.selenium.support.ui.Select 首先打包,然后创建 Select 实例并调用上述任何方法。

.png)
.png)
.png)
.png)