- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境設定
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - 操作命令
- Selenium - 訪問器命令
- Selenium - 斷言命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼輸出
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver 與 RC
- Selenium - 安裝
- Selenium - 第一個測試指令碼
- Selenium - 驅動程式會話
- Selenium - 瀏覽器選項
- Selenium - Chrome 選項
- Selenium - Edge 選項
- Selenium - Firefox 選項
- Selenium - Safari 選項
- Selenium - 雙擊
- Selenium - 右鍵點選
- Python 中的 HTML 報告
- 處理編輯框
- Selenium - 單個元素
- Selenium - 多個元素
- Selenium Web 元素
- Selenium - 檔案上傳
- Selenium - 定位器策略
- Selenium - 相對定位器
- Selenium - 查詢器
- Selenium - 查詢所有連結
- Selenium - 使用者互動
- Selenium - WebElement 命令
- Selenium - 瀏覽器互動
- Selenium - 瀏覽器命令
- Selenium - 瀏覽器導航
- Selenium - 警報和彈出視窗
- Selenium - 處理表單
- Selenium - 視窗和標籤頁
- Selenium - 處理連結
- Selenium - 輸入框
- Selenium - 單選按鈕
- Selenium - 複選框
- Selenium - 下拉框
- Selenium - 處理 IFrame
- Selenium - 處理 Cookie
- Selenium - 日期時間選擇器
- Selenium - 動態 Web 表格
- Selenium - Actions 類
- Selenium - Action 類
- Selenium - 鍵盤事件
- Selenium - 鍵上/下
- Selenium - 複製和貼上
- Selenium - 處理特殊鍵
- Selenium - 滑鼠事件
- Selenium - 拖放
- Selenium - 筆事件
- Selenium - 滾動操作
- Selenium - 等待策略
- Selenium - 顯式/隱式等待
- Selenium - 支援功能
- Selenium - 多選
- Selenium - 等待支援
- Selenium - 選擇支援
- Selenium - 顏色支援
- Selenium - ThreadGuard
- Selenium - 錯誤和日誌記錄
- Selenium - 異常處理
- Selenium - 雜項
- Selenium - 處理 Ajax 呼叫
- Selenium - JSON 資料檔案
- Selenium - CSV 資料檔案
- Selenium - Excel 資料檔案
- Selenium - 跨瀏覽器測試
- Selenium - 多瀏覽器測試
- Selenium - 多視窗測試
- Selenium - JavaScript 執行器
- Selenium - 無頭執行
- Selenium - 捕獲螢幕截圖
- Selenium - 捕獲影片
- Selenium - 頁面物件模型
- Selenium - 頁面工廠
- Selenium - 記錄和回放
- Selenium - 框架
- Selenium - 瀏覽上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架構
- Selenium - 元件
- Selenium - 配置
- Selenium - 建立測試指令碼
- Selenium - 測試執行
- Selenium - 端點
- Selenium - 自定義節點
- Selenium 報告工具
- Selenium - 報告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium 與其他技術
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven 和 Jenkins
- Selenium - 資料庫測試
- Selenium - LogExpert 日誌記錄
- Selenium - Log4j 日誌記錄
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 測試
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium 雜項概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 關鍵字驅動框架
- Selenium - 資料驅動框架
- Selenium - 混合驅動框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
Selenium WebDriver - 選擇支援
Selenium WebDriver 可以使用 Select 類來選擇網頁上下拉框中的選項。網頁上可能有兩種型別的下拉框 - 單選(允許選擇一個選項)和多選(允許選擇多個選項)。
下拉框由稱為 select 的標籤名稱識別。此外,其每個選項都具有稱為 option 的標籤名稱。此外,多選下拉框包含一個屬性 multiple。
HTML 中的下拉框識別
右鍵點選網頁,然後點選 Chrome 瀏覽器中的“檢查”按鈕。然後,整個頁面的相應 HTML 程式碼將可見。為了檢查網頁上的下拉框,我們點選左側向上的箭頭,該箭頭位於可見的 HTML 程式碼頂部,如下所示。
點選後,將箭頭指向文字選擇一個旁邊的下拉框,其 HTML 程式碼可見,反映了 select 標籤名稱(括在 <> 中),以及 option 標籤名稱內的選項。
請注意,其中一個選項選擇一個標題具有屬性 selected,這意味著預設情況下會選擇此選項,即使在選擇任何選項之前也是如此。此外,如果下拉列表包含一個屬性值 disabled,則該選項將無法選擇。
基本選擇方法
Selenium WebDriver 的 Select 類中提供了多種方法,可幫助我們處理多選和單選下拉框。
示例 1 - 單選下拉框
讓我們以以下頁面為例,我們將訪問文字選擇一個下方的下拉框,選擇值其他,並使用上面討論的方法執行一些驗證。
程式碼實現
package org.example;
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 java.util.List;
import java.util.concurrent.TimeUnit;
public class SelectDropdowns {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Opening the webpage where we will get dropdown
driver.get("https://tutorialspoint.tw/selenium/practice/select-menu.php");
// identify dropdown then select its options by index
WebElement dropdown = driver.findElement
(By.xpath("//*[@id='inputGroupSelect03']"));
Select select = new Select(dropdown);
// get option selected by default
WebElement o = select.getFirstSelectedOption();
System.out.println("Option selected by default: " + o.getText());
// select an option by index
select.selectByIndex(6);
// get selected option
List<WebElement> selectedOptions = select.getAllSelectedOptions();
for (WebElement opt : selectedOptions){
System.out.println("Selected Option is: " + opt.getText());
}
// get all options of dropdown
List<WebElement> options =select.getOptions();
for (WebElement opt : options){
System.out.println("Options are: " + opt.getText());
}
// check if multiselect dropdown
Boolean b = select.isMultiple();
System.out.println("Boolean value for checking is: "+ b);
// quitting browser
driver.quit();
}
}
輸出
Option selected by default: Pick one title Selected Option is: Other Options are: Pick one title Options are: Dr. Options are: Mr. Options are: Mrs. Options are: Ms. Options are: Proof. Options are: Other Boolean value for checking is: false Process finished with exit code 0
在上面的示例中,我們透過控制檯中的訊息獲取了下拉框中選定的選項 - 選定的選項是:其他。然後獲取下拉框的所有選項,控制檯中的訊息為 -選項是:選擇一個標題,選項是:博士,選項是:非洲,選項是:先生,選項是:女士,選項是:小姐,選項是:證明,和選項是:其他。
我們還驗證了下拉框沒有多個選擇選項,控制檯中的訊息為 - 檢查的布林值為:false。我們還檢索了下拉框中預設選定的選項,控制檯中的訊息為 - 預設選定的選項:選擇一個標題。
最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。
示例 2 - 多選下拉框
讓我們再以以下頁面為例,我們將訪問文字多選下拉框旁邊的多選下拉框,選擇值電子產品和電腦和運動和戶外,並使用上面討論的方法執行一些驗證。
程式碼實現
package org.example;
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 java.util.List;
import java.util.concurrent.TimeUnit;
public class SelectMultipleDropdowns {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Opening the webpage where we will get dropdown
driver.get("https://tutorialspoint.tw/selenium/practice/select-menu.php");
// identify multiple dropdown
WebElement dropdown = driver.findElement
(By.xpath("//*[@id='demo-multiple-select']"));
// object of Select class
Select select = new Select(dropdown);
// gets options of dropdown in list
List<WebElement> options = select.getOptions();
for (WebElement opt : options){
System.out.println("Options are: " + opt.getText());
}
// return true if multi-select dropdown
Boolean b = select.isMultiple();
System.out.println("Boolean value for multiple dropdown: "+ b);
// select item by value
select.selectByValue("3");
// select item by index
select.selectByIndex(7);
// get all selected options of dropdown in list
List<WebElement> selectedOptions = select.getAllSelectedOptions();
for (WebElement opt : selectedOptions){
System.out.println("Selected Options are: " + opt.getText());
}
// get first selected option in dropdown
WebElement f = select.getFirstSelectedOption();
System.out.println("First selected option is: "+ f.getText());
// deselect option by index
select.deselectByIndex(7);
// deselect all selected items
select.deselectAll();
// get all selected options of dropdown after deselected
List<WebElement> delectedOptions = select.getAllSelectedOptions();
System.out.println("No. options selected: " + delectedOptions.size());
// Closing browser
driver.quit();
}
}
輸出
Options are: Books Options are: Movies, Music & Games Options are: Electronics & Computers Options are: Home, Garden & Tools Options are: Health & Beauty Options are: Toys, Kids & Baby Options are: Clothing & Jewelry Options are: Sports & Outdoors Boolean value for multiple dropdown: true Selected Options are: Electronics & Computers Selected Options are: Sports & Outdoors First selected option is: Electronics & Computers No. options selected: 0 Process finished with exit code 0
在上面的示例中,我們透過控制檯中的訊息獲取了下拉框的所有選項 - 選項是:書籍,選項是:電影,音樂和遊戲,選項是:家居,花園和工具,選項是:健康和美容,選項是:玩具,兒童和嬰兒,選項是:服裝和珠寶,選項是:運動和戶外。
我們還驗證了下拉框有多個選擇選項,控制檯中的訊息為 - 檢查的布林值為:true。我們透過控制檯中的訊息檢索了下拉框中選定的選項 - 選定的選項是:電子產品和電腦,選定的選項是:運動和戶外。
我們還透過控制檯中的訊息獲取了第一個選定的選項 - 第一個選定的選項是:電子產品和電腦。最後,我們取消選擇下拉框中所有選定的選項,因此在控制檯中獲取了訊息 - 已選選項數:0。
最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。
這總結了我們關於 Selenium WebDriver - 選擇支援教程的全面內容。我們從描述 HTML 中下拉框的識別、基本選擇方法以及示例開始,說明如何在 Selenium WebDriver 中處理單選和多選下拉框。
這使您對 Selenium WebDriver - 選擇支援有了深入的瞭解。明智的做法是不斷練習您所學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並擴充套件您的視野。
