• Selenium Video Tutorials

Selenium WebDriver - 複選框



Selenium Webdriver 可用於處理網頁上的複選框。所有複選框都使用 input 標籤名稱進行識別。此外,網頁上的每個複選框都具有一個名為 type 的屬性,其值為 checkbox

在網頁上識別複選框

開啟 Chrome 瀏覽器,並啟動一個應用程式。右鍵單擊網頁,然後單擊“檢查”按鈕。要識別頁面上的複選框,請單擊可用 HTML 程式碼頂部左側的向上箭頭,如下所示。

Selenium Checkbox 1

一旦我們單擊並將箭頭指向複選框(在下圖中突出顯示),其 HTML 程式碼就會出現,反映了 input 標籤名稱和 type 屬性的值為複選框。

Selenium Checkbox 2

選擇複選框並驗證

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

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

廣告

© . All rights reserved.