Selenium 简明教程
Selenium WebDriver - Checkboxes
Selenium Webdriver 可用于处理网页上的复选框。所有复选框都使用 input 标签名进行标识。此外,网页上的每个复选框都有一个名为 type 的属性,其值为 checkbox 。
Selenium Webdriver can be used to handle checkboxes on a web page. All checkboxes are identified using the input tagname. Also, each checkbox on a web page has an attribute called type with its value as checkbox.
Identify Checkbox on a Web Page
打开 Chrome 浏览器并启动一个应用程序。右键单击网页,然后单击“检查”按钮。要识别页面上的复选框,请单击可用 HTML 代码顶部的左上方箭头,如下所示。
Open the Chrome browser, and launch an application. Right click on the webpage then click on the Inspect button. For identifying the checkbox on the page, click on the left upward arrow, at the top of the available HTML code as highlighted below.
一旦我们单击并指向复选框(在下面的图片中突出显示),它的 HTML 代码就会出现,反映了 input 标签名和 type 属性值为 checkbox。
Once we had clicked and pointed the arrow to the checkbox(highlighted in the below image), its HTML code appeared, reflecting both the input tagname and value of type attribute as checkbox.
Select Checkbox and Verify
让我们举一个上面页面的示例,我们将在 click() 方法的帮助下单击第一个复选框。然后我们将使用 isSelected() 方法验证复选框是否已选中。
Let us take an example of the above page, where we would click the first checkbox with the help of the click() method. Then we would verify if the checkbox is checked using the isSelected() method.
要获取有关 isSelected() 方法的更多信息,请参阅链接 Selenium WebDriver WebElement Commands 。
To get more information about isSelected() method, please refer to the link Selenium WebDriver WebElement Commands.
Syntax
Webdriver driver = new ChromeDriver();
WebElement checkbox= driver.findElement(By.xpath("value of xpath"));
checkbox.click();
boolean result = checkBox.isSelected();
Example
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://www.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
在上面的示例中,我们首先单击了第一个复选框,然后使用消息 - Checking if a checkbox is selected: true 在控制台中验证了该复选框是否已选中。
In the above example, we had first clicked on the first checkbox, and then verified if the checkbox was checked in the console with the message - Checking if a checkbox is selected: true.
最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Count Total Checkboxes
让我们举另一个下面页面的示例,我们将计算复选框的总数。在此示例中,复选框总数应为 3。
Let us take another example of the below page, where we would count the total number of checkboxes. In this example, the total number of checkboxes should be 3.
Syntax
Webdriver driver = new ChromeDriver();
List<WebElement> totalChks = driver.findElements
(By.xpath("<xpath value of all checkboxes>"));
int count = totalChks.size();
Example
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://www.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
在上面的示例中,我们计算了网页上的复选框总数,并在控制台中收到了消息 - Count the checkboxes: 3 。
In the above example, we had counted the total number of checkboxes on a webpage, and received the messages in the console - Count the checkboxes: 3.
Validate Checkboxes
让我们再举一个有关复选框的示例,我们将对复选框执行一些验证。首先,我们将使用 isEnabled() 方法检查复选框是否已启用/禁用。此外,我们将分别使用 isDisplayed() 和 isSelected() 方法验证它是否已显示和选中/未选中。
Let us take another example on checkboxes, where we would perform some validation on the checkboxes. First, we would check if the checkbox is enabled/disabled using the isEnabled() method. Also, we would verify if it is displayed and checked/unchecked using the isDisplayed() and isSelected() methods respectively.
Syntax
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);
Example
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://www.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
在上面的示例中,我们验证了一个复选框是否已显示、启用和选中,并在控制台中收到了以下消息 - Checking if a checkbox is selected: false, Checking if a checkbox is displayed: true and Checking if a checkbox is enabled: true 。
In the above example, we had validated if a checkbox was displayed, enabled, and selected, and received the following messages in the console - Checking if a checkbox is selected: false, Checking if a checkbox is displayed: true and Checking if a checkbox is enabled: true.
Conclusion
这些是有关 Selenium WebDriver 复选框教程的全面思考的结论。我们首先介绍了 HTML 中复选框的识别,提供了说明如何处理 Selenium WebDriver 中复选框的示例。这会为你提供有关 Selenium WebDriver 复选框的深入知识。明智的做法是不断练习你所学到的知识,并探索与 Selenium 相关以加深理解和扩展视野的其他内容。
This concludes our comprehensive take on the tutorial on Selenium Webdriver Checkboxes. We’ve started with describing identification of checkboxes in HTML, and examples to illustrate how to handle checkboxes in Selenium Webdriver. This equips you with in-depth knowledge of the Selenium Webdriver Checkboxes. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.