如何在 Selenium 中从下拉列表中选择值

如何在 Selenium 中选择下拉菜单

以下是在 Selenium 中如何从下拉菜单中选择值的分步过程

在处理 Selenium 中的下拉菜单和控制下拉框之前,我们必须做以下两件事

  1. 导入包 org.openqa.selenium.support.ui.Select
  2. 将下拉框实例化为对象,在 Selenium WebDriver 中选择

例如,转到 Mercury Tours 的注册页面 (https://demo.guru99.com/test/newtours/register.php) 并注意其中的“国家”下拉框。

Select Dropdown in Selenium

步骤 1) 导入“Select”包。

import org.openqa.selenium.support.ui.Select;

步骤 2) 将下拉元素声明为 Select 类的实例.

在下面的示例中,我们将此实例命名为“drpCountry”。

Select drpCountry = new Select(driver.findElement(By.name("country")));

步骤 3) 开始控制它。

我们现在可以通过使用任何可用的 Select 方法在 Selenium 中选择下拉菜单来控制“drpCountry”。下面的示例代码将选择选项“ANTARCTICA”。

drpCountry.selectByVisibleText("ANTARCTICA");

Selenium 中的选择类

Selenium 中的 Select 类是一种用于实现 HTML SELECT 标签的方法。html select 标签提供了选择和取消选择元素的辅助方法。Select 类是一个普通类,因此使用 New 关键字创建其对象并指定 Web 元素位置。

Selenium 中的选择方法

以下是 Selenium 下拉列表中最常用的方法。

#1) selectByVisibleText() 和 deselectByVisibleText()

  • 选择/取消选择显示与参数匹配文本的选项。
  • 参数:特定选项的确切显示文本

示例

drpCountry.selectByVisibleText("ANTARCTICA");

#2) selectByValue() 和 deselectByValue()

  • 选择/取消选择“value”属性与指定参数匹配的选项。
  • 请记住,并非所有下拉选项都具有相同的文本和“值”,如下面的示例所示。
  • 参数:“value”属性的值

示例

SelectByValue and deselectbyvalue

drpCountry.selectByValue("234");

#3) selectByIndex() 和 deselectByIndex()

  • 选择/取消选择给定索引处的选项。
  • 参数:要选择的选项的索引。

示例

drpCountry.selectByIndex(0);

#4) isMultiple()

  • 如果下拉元素允许同时进行多项选择,则返回 TRUE;否则返回 FALSE。
  • 参数:不需要

示例

if (drpCountry.isMultiple())
{
//do something here
}

#5) deselectAll()

  • 清除所有选定的条目。这仅在下拉元素支持多项选择时有效。
  • 参数:不需要

示例

drpCountry.deselectAll();

Selenium 中选择方法的完整代码

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);

		Select drpCountry = new Select(driver.findElement(By.name("country")));
		drpCountry.selectByVisibleText("ANTARCTICA");

		//Selecting Items in a Multiple SELECT elements
		driver.get("http://jsbin.com/osebed/2");
		Select fruits = new Select(driver.findElement(By.id("fruits")));
		fruits.selectByVisibleText("Banana");
		fruits.selectByIndex(1);
 }
}

在多个 SELECT 元素中选择项目

我们还可以使用 selectByVisibleText() 方法在多 SELECT 元素中选择多个选项。例如,我们将 https://jsbin.com/osebed/2 作为基本 URL。它包含一个允许同时进行多项选择的下拉框。

Selecting Items In A Multiple Select Elements

以下代码将使用 selectByVisibleText() 方法选择前两个选项。

Selecting Items In A Multiple Select Elements

摘要

命令 描述
selectByVisibleText()/

deselectByVisibleText()

按其显示文本选择/取消选择选项
selectByValue()/

deselectByValue()

按其“value”属性的值选择/取消选择选项
selectByIndex()/

deselectByIndex()

按其索引选择/取消选择选项
isMultiple() 如果下拉元素允许同时进行多项选择,则返回 TRUE;否则返回 FALSE
deselectAll() 取消选择所有先前选择的选项

要控制下拉框,您必须首先导入 org.openqa.selenium.support.ui.Select 包,然后创建 Select 实例。