• Selenium Video Tutorials

Selenium WebDriver - 單選按鈕



Selenium WebDriver 可用於處理網頁上的單選按鈕。在 HTML 術語中,每個單選按鈕都由名為 input 的標籤名標識。此外,網頁上的每個單選按鈕都將始終具有名為 type 的屬性,其值為 radio

HTML 中單選按鈕的標識

啟動瀏覽器(例如 Chrome),右鍵單擊網頁,然後單擊“檢查”按鈕。要識別頁面上的單選按鈕,請單擊如下所示突出顯示的左上方箭頭。

Selenium Button Radio 1

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

Selenium Button Radio 2

單擊並選中單選按鈕

讓我們以以上頁面為例,我們將使用 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。

Selenium Radio 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 相關的其他內容,以加深您的理解並拓寬您的視野。

廣告