- 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 vs 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 可用於處理網頁上的複選框。所有複選框都使用 input 標籤名稱進行識別。此外,網頁上的每個複選框都具有一個名為 type 的屬性,其值為 checkbox。
在網頁上識別複選框
開啟 Chrome 瀏覽器,並啟動一個應用程式。右鍵單擊網頁,然後單擊“檢查”按鈕。要識別頁面上的複選框,請單擊可用 HTML 程式碼頂部左側的向上箭頭,如下所示。
一旦我們單擊並將箭頭指向複選框(在下圖中突出顯示),其 HTML 程式碼就會出現,反映了 input 標籤名稱和 type 屬性的值為複選框。
選擇複選框並驗證
讓我們以上面頁面的示例為例,我們將使用 click() 方法單擊第一個複選框。然後,我們將使用 isSelected() 方法驗證複選框是否已選中。
要獲取有關 isSelected() 方法的更多資訊,請參閱連結 Selenium WebDriver WebElement 命令。
語法
Webdriver driver = new ChromeDriver();
WebElement checkbox= driver.findElement(By.xpath("value of xpath"));
checkbox.click();
boolean result = checkBox.isSelected();
示例
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 java.util.concurrent.TimeUnit;
public class HandlingCheckbox {
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 identify checkbox
driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");
// identify the first checkbox
WebElement checkBox = driver.findElement(By.xpath("//*[@id='hobbies']"));
// click the checkbox
checkBox.click();
// check if a checkbox is selected
boolean result = checkBox.isSelected();
System.out.println("Checking if a checkbox is selected: " + result);
// Closing browser
driver.quit();
}
}
輸出
Checking if a checkbox is selected: true Process finished with exit code 0
在上面的示例中,我們首先單擊了第一個複選框,然後在控制檯中驗證了複選框是否已選中,訊息為 - 檢查複選框是否已選中:true。
最後,收到訊息 程序已完成,退出程式碼為 0,表示程式碼已成功執行。
計算總複選框數
讓我們以下面頁面的另一個示例為例,我們將計算複選框的總數。在此示例中,複選框的總數應為 3。
語法
Webdriver driver = new ChromeDriver();
List<WebElement> totalChks = driver.findElements
(By.xpath("<xpath value of all checkboxes>"));
int count = totalChks.size();
示例
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 java.util.concurrent.TimeUnit;
import java.util.List;
public class CountingCheckbox {
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 identify checkbox
driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");
// Retrieve all checkboxes using locator and storing in List
List<WebElement> totalChks = driver.findElements(By.xpath("//input[@type='checkbox']"));
// count number of checkboxes
int count = totalChks.size();
System.out.println("Count the checkboxes: " + count);
//Closing browser
driver.quit();
}
}
輸出
Count the checkboxes: 3
在上面的示例中,我們計算了網頁上覆選框的總數,並在控制檯中收到了訊息 - 計算複選框數:3。
驗證複選框
讓我們再舉一個關於複選框的例子,我們將對複選框執行一些驗證。首先,我們將使用 isEnabled() 方法檢查複選框是否已啟用/停用。此外,我們還將分別使用 isDisplayed() 和 isSelected() 方法驗證它是否顯示以及是否已選中/未選中。
語法
Webdriver driver = new ChromeDriver();
// identify checkbox but not selected
WebElement checkBox = driver.findElement(By.xpath("<xpath value of checkbox>"));
// verify if checkbox is selected
boolean result = checkBox.isSelected();
System.out.println("Checking if a checkbox is selected: " + result);
// verify if checkbox is displayed
boolean result1 = checkBox.isDisplayed();
System.out.println("Checking if a checkbox is displayed: " + result1);
// verify if checkbox is enabled
boolean result2 = checkBox.isEnabled();
System.out.println("Checking if a checkbox is enabled: " + result2);
示例
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 java.util.concurrent.TimeUnit;
public class CheckboxValidates {
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 identify checkbox
driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");
// identify checkbox but not select
WebElement checkBox = driver.findElement
(By.xpath("//*[@id='practiceForm']/div[7]/div/div/div[2]/input"));
// verify if checkbox is selected
boolean result = checkBox.isSelected();
System.out.println("Checking if a checkbox is selected: " + result);
// verify if checkbox is displayed
boolean result1 = checkBox.isDisplayed();
System.out.println("Checking if a checkbox is displayed: " + result1);
// verify if checkbox is enabled
boolean result2 = checkBox.isEnabled();
System.out.println("Checking if a checkbox is enabled: " + result2);
// Closing browser
driver.quit();
}
}
輸出
Checking if a checkbox is selected: false Checking if a checkbox is displayed: true Checking if a checkbox is enabled: true
在上面的示例中,我們驗證了複選框是否顯示、啟用和選中,並在控制檯中收到了以下訊息 - 檢查複選框是否已選中:false,檢查複選框是否已顯示:true 和檢查複選框是否已啟用:true。
結論
這總結了我們關於 Selenium Webdriver 複選框教程的全面介紹。我們從描述 HTML 中複選框的識別開始,並提供了一些示例來說明如何在 Selenium Webdriver 中處理複選框。這使您能夠深入瞭解 Selenium Webdriver 複選框。明智的做法是不斷練習您所學到的知識,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。
