
- 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 標籤名內的選項及其屬性 multiple。

Select 中的基本多選方法
Selenium webdriver 的 Select 類中提供了多種方法,可以幫助我們處理這兩種多選下拉選單。
示例
讓我們再舉一個以下頁面的例子,在這裡我們將訪問文字多選下拉選單旁邊的多選下拉選單,選擇值電影、音樂和遊戲、電子產品和電腦以及書籍,並使用上面討論的方法執行一些驗證。

程式碼實現
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 HandlingMultDropdown { 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 visible text select.selectByVisibleText("Books"); // select item by value select.selectByValue("2"); // select item by index select.selectByIndex(2); // 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(2); // get all selected options of dropdown after deselecting 1 List<WebElement> deletedOptions = select.getAllSelectedOptions(); System.out.println("No. options selected after deselecting one option: " + deletedOptions.size()); // deselect all selected items select.deselectAll(); // get all selected options of dropdown after deselected List<WebElement> deletedAllOptions = select.getAllSelectedOptions(); System.out.println("No. options selected after deselecting all: " + deletedAllOptions.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: Books Selected Options are: Movies, Music & Games Selected Options are: Electronics & Computers First selected option is: Books No. options selected after deselecting one option: 2 No. options selected after deselecting all: 0 Process finished with exit code 0
在上面的示例中,我們使用控制檯中的訊息獲取了下拉選單的所有選項 - 選項為:書籍,選項為:電影、音樂和遊戲,選項為:家居、園藝和工具,選項為:健康和美容,選項為:玩具、兒童和嬰兒,選項為:服裝和珠寶,選項為:運動和戶外。
我們還使用控制檯中的訊息驗證了下拉選單具有多個選擇選項 - 用於檢查的布林值為:true。我們使用控制檯中的訊息檢索了下拉選單中的選定選項 - 選定的選項為:書籍,選定的選項為:電影、音樂和遊戲,以及選定的選項為:電子產品和電腦。
我們還使用控制檯中的訊息獲取了第一個選定的選項 - 第一個選定的選項為:書籍。取消選擇一個選定的選項後,我們使用控制檯中的訊息收到了選定選項的數量 - 取消選擇一個選項後選定的選項數:2。
最後,我們取消選擇了下拉選單中所有選定的選項,因此在控制檯中獲得了訊息 - 取消選擇所有選項後選定的選項數:0。
最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。
這總結了我們對 Selenium Webdriver - 多選教程的全面介紹。我們從描述 HTML 中多選下拉選單的識別、Select 中的基本多選方法以及一個示例來說明如何在 Selenium Webdriver 中處理多選下拉選單開始。
這為您提供了 Selenium Webdriver - 多選的深入知識。明智的做法是不斷練習您所學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並拓寬您的視野。