
- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境搭建
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 功能特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- 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 WebDriver - 單選按鈕
Selenium WebDriver 可用於處理網頁上的單選按鈕。在 HTML 術語中,每個單選按鈕都由名為 input 的標籤名標識。此外,網頁上的每個單選按鈕都將始終具有名為 type 的屬性,其值為 radio。
HTML 中單選按鈕的標識
啟動瀏覽器(例如 Chrome),右鍵單擊網頁,然後單擊“檢查”按鈕。要識別頁面上的單選按鈕,請單擊如下所示突出顯示的左上方箭頭。

單擊箭頭指向單選按鈕(如下圖所示突出顯示)後,其 HTML 程式碼將可見,反映了 input 標籤名和 type 屬性的值為 radio。

單擊並選中單選按鈕
讓我們以以上頁面為例,我們將使用 click() 方法單擊其中一個單選按鈕。然後,我們將使用 isSelected() 方法驗證單選按鈕是否被選中。此方法返回布林值(true 或 false)。如果單選按鈕被選中,isSelected() 將返回 true,否則返回 false。
示例
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 HandlingRadioButton { 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 radio button driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php"); // identify radio button then click WebElement radiobtn = driver.findElement(By.xpath("//*[@id='gender']")); radiobtn.click(); // verify if radio button is selected boolean result = radiobtn.isSelected(); System.out.println("Checking if a radio button is selected: " + result); // Closing browser driver.quit(); } }
輸出
Checking if a radio button is selected: true Process finished with exit code 0
在上面的示例中,我們首先單擊了一個單選按鈕,然後在控制檯中驗證了該單選按鈕是否被選中,訊息為 - 正在檢查單選按鈕是否被選中:true。
最後,收到訊息 程序已完成,退出程式碼為 0,表示程式碼已成功執行。
統計單選按鈕數量
讓我們以以下頁面為例,我們將計算單選按鈕的總數。在這個例子中,單選按鈕總數應為 3。

示例
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.List; import java.util.concurrent.TimeUnit; public class HandlingRadioButton { 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 radio button driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php"); // Retrieve all radio buttons using locator and storing in List List<WebElement> totalRadioBtns = driver.findElements(By.xpath("//input[@type='radio']")); // count number of radio buttons int count = totalRadioBtns.size(); System.out.println("Count the radio buttons: " + count); // Closing browser driver.quit(); } }
輸出
Count the radio buttons: 3
在上面的示例中,我們計算了網頁上單選按鈕的總數,並在控制檯中收到訊息 - 單選按鈕計數:3。
驗證單選按鈕
讓我們以上述網頁為例,我們將對單選按鈕執行一些驗證。首先,我們將使用 isEnabled() 方法檢查單選按鈕是否啟用/停用。此外,我們將分別使用 isDisplayed() 和 isSelected() 方法驗證它是否顯示和選中/未選中。
這些方法返回布林值(true 或 false)。如果單選按鈕被選中、啟用和顯示,則返回 true,否則返回 false。
示例
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 ValidateRadioButton { 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 click radio button driver.get("https://tutorialspoint.tw/selenium/selenium_automation_practice.htm"); // identify radio button then click WebElement radiobtn = driver.findElement (By.xpath("//*[@id='practiceForm']/div[3]/div/div/div[2]/input")); radiobtn.click(); // verify if radio button is selected boolean result = radiobtn.isSelected(); System.out.println("Checking if a radio button is selected: " + result); // verify if radio button is displayed boolean result1 = radiobtn.isDisplayed(); System.out.println("Checking if a radio button is displayed: " + result1); // verify if radio button is enabled boolean result2 = radiobtn.isEnabled(); System.out.println("Checking if a radio button is enabled: " + result2); // identify another radio button WebElement radiobtn1 = driver.findElement(By.xpath("//*[@id='gender']")); // verify if radio button is not selected boolean result3 = radiobtn1.isSelected(); System.out.println("Checking if the other radio button is unselected: " + result3); // Closing browser driver.quit(); } }
輸出
Checking if a radio button is selected: true Checking if a radio button is displayed: true Checking if a radio button is enabled: true Checking if the other radio button is unselected: false
在上面的示例中,我們驗證了一個單選按鈕是否顯示、啟用和選中,並在控制檯中收到以下訊息:正在檢查單選按鈕是否被選中:true,正在檢查單選按鈕是否顯示:true,正在檢查單選按鈕是否啟用:true 和 正在檢查另一個單選按鈕是否未選中:false。
結論
本教程全面介紹了 Selenium WebDriver 單選按鈕。我們從描述 HTML 中單選按鈕的標識開始,並透過示例來說明如何在 Selenium WebDriver 中處理單選按鈕。這將為您提供 Selenium WebDriver 單選按鈕的深入知識。明智的做法是繼續練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。