使用Selenium在WebDriver中處理下拉選單和多選
我們可以使用Selenium WebDriver中的Select類來處理多選下拉選單。多選下拉選單允許選擇多個選項。
下面列出了處理多選下拉選單的Select方法:
getOptions – 返回下拉選單中所有選項的列表。
Select s = new Select(e); List<WebElement> l = s.getOptions();
getFirstSelectedOption – 返回下拉選單中選定的選項。如果有多個選項被選中,則只返回第一個專案。
Select s = new Select(e); l = s. getFirstSelectedOption();
isMultiple – 返回布林值,如果下拉選單允許選擇多個專案,則返回true。
Select s = new Select(e); boolean l = s.isMultiple();
selectByIndex – 以引數形式傳遞要由下拉選單選擇的選項的索引。索引從0開始。
WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.selectByIndex(0);selectByValue – 以引數形式傳遞要由下拉選單選擇的選項的value屬性。下拉選單中的選項應具有value屬性,以便可以使用此方法。
WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.selectByValue("option 1");deselectByVisibleText – 以引數形式傳遞要由下拉選單取消選擇的選項的可見文字。它僅適用於多選下拉選單。
WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.deselectByVisibleText("Selenium");deselectByIndex – 以引數形式傳遞要由下拉選單取消選擇的選項的索引。索引從0開始。它僅適用於多選下拉選單。
WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.deselectByIndex(0);deselectByValue – 以引數形式傳遞要由下拉選單取消選擇的選項的value屬性。它僅適用於多選下拉選單。
WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.deselectByValue("option 1");selectByVisibleText – 以引數形式傳遞要由下拉選單選擇的選項的可見文字。
WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.selectByVisibleText("Selenium");deselectAll – 取消選擇下拉選單中所有選定的選項。
WebElement e = driver.findElement(By.id("slc"));
Select s = new Select(e);
s.deselectAll();多選下拉選單具有一個select標籤,其專案由option標籤表示。

示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.Select
public class MultiDrpDwn{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",
"C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//implicit wait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//maximize browser
driver.manage().window().maximize();
//URL launch
driver.get("https://tutorialspoint.tw/selenium/selenium_automation_practice.htm");
//identify dropdown
WebElement d= driverfindElement(By.xpath("//select[@name='selenium_commands']"));
//object of Select class
Select s=new Select(d);
//get options of dropdown in list
List t =s.getOptions();
System.out.println("Options are: ");
for (WebElement i: t){
System.out.println(i.getText());
}
//return true if multi-select dropdown
Boolean b=s.isMultiple();
System.out.println("Boolen value for drodown: "+ b);
//select item by index
s.selectByIndex(2);
Thread.sleep(1000);
//select item by visible text
s.selectByVisibleText("Wait Commands");
Thread.sleep(1000);
//get first selected option in dropdown
WebElement f = s.getFirstSelectedOption();
System.out.println("First selected option is: "+ f.getText());
//deselect option by index
s.deselectByIndex(2);
Thread.sleep(1000);
//deselect all selected items
s.deselectAll();
driver.close();
}
}輸出

廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP