• 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.com/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,表示代码已成功执行。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

计算总复选框数

让我们以下面页面的另一个示例为例,我们将计算复选框的总数。在此示例中,复选框的总数应为 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.com/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.com/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 相关的其他内容,以加深您的理解并拓宽您的视野。

广告