How to Select a Checkbox In Selenium WebDriver And Do Other Validations

In this article,I am going to show you how to select a Checkbox in Selenium WebDriver and perform additional validations like whether the Checkbox is Enabled and is Displayed on the page.

We will consider the following DOM structure for the examples.

Select a Checkbox In Selenium WebDriverVarious Actions on a Checkbox in Selenium WebDriver

Select a Checkbox In Selenium WebDriver By ID

We can select a checkbox by first locating it and then perform a click action to select it. In the following example, I am selecting the Check Box 1 option.

Select a Checkbox Using CSS Selector ID attribute

The following example selects the Check Box 2 using the CSS Selector ID attribute. To more about CSS Selector locator please refer to my post How to Use CSS Selector in Selenium WebDriver | 13 Examples.

Select a Checkbox Using CSS Selector Class attribute

The following example selects the Check Box 3 using the CSS Selector ID attribute. 

Select a Checkbox Using XPath In Selenium WebDriver

The following example selects Check Box 1 using the XPath by its ID property. To know more about XPath please refer to my post XPath in Selenium WebDriver with Examples.

Select a Checkbox Using XPath When a Unique property is not found

In the above example, we are easily able to select Check Box 1 using XPath by its ID as all the checkboxes are having unique ID properties. But in real projects, you might come across scenarios wherein it becomes very difficult or impossible to find a unique property like ID or name and etc for a web element. If you look at the HTML Tags of the checkboxes you will observe that all the checkboxes are having the same name “C1“.If we try to select the second or third checkbox using its name it won’t work. Selenium Webdriver will always the first check box as it comes first in the DOM.So in the following example, I will show you how to locate the second checkbox that is the “Check Box 2″ using XPath by its name which is not a unique property.

How to check whether a Checkbox is Visible and Enabled on the Page?

Whenever you are going to perform any action on a web element it is standard practice to first check whether it is visible on the page. If it is visible the next thing we should check whether it is enabled or not. The following example illustrates how to do it. Here we will first check whether Check Box 1 is visible on the page using isDisplayed method. If it is visible we will check whether it is in the enabled state using the isEnabled method. If it is enabled we will select it.

How to Verify whether Checkbox is Selected?

There may be a requirement to check the default state of a checkbox or you want to check whether the checkbox was actually selected after performing the select action. We can use the isSelected() method to get the current state of the checkbox, whether it is selected or not. 

Conclusion

  • We can select Checkbox by using the click() method.
  • We can locate the Checkbox elements by id, name, CSS, and XPath selector.
  • We can perform validation operations like isDisplayed(), isSelected(), and isEnabled() before selecting the Checkbox to get rid of runtime exceptions thrown by Selenium WebDriver.

Recommended Posts

Leave a Reply