Selenium Tutorial for Beginners

How To Select a Checkbox With Selenium WebDriver

Continuing on our Selenium Beginner’s series, let’s see How to Select a Checkbox with Selenium WebDriver using Java. Hope you have already gone through the previous tutorials…

Here we will try to select ‘Option 1’ checkbox on Selenium Framework practice form.

Select a Checkbox with Selenium WebDriver

Inspect the Element first

This can be done by using browser tools. In simple terms just right click on the element (Options checkbox) & select ‘Option 1’. Note down the ‘attribute-value’ pairs, in this case name, id, value & label.

How to Select a Checkbox with Selenium WebDriver

 

<< THE CODE >>

The steps followed are,

  1. Open a browser
  2. Open the website URL & maximize the window
  3. Find the ‘Options’ checkboxes
  4. Select the ‘Option 1’ checkbox

Open Browser & Application

The code till,

is to open the browser and application. For reference, please visit our earlier posts in the series.

Identify the Checkbox & its options

Since a set of checkbox will have multiple options to select, we need to make a list of WebElements using the property which is same for all the checkboxes – in this case ‘name=” vfb-6[]”.

An element can be identified using different properties. Please note that we need to take a property that is same for all the checkboxes. For details on different element locators, click here… After identifying the checkbox options, we are saving it in the ‘Checkbox’ instance of type ‘WebElement’ which is an in-built class available in Selenium. We need to import this class in order to create objects,

Check a Radio button with Selenium WebDriver

Since it’s a list of WebElements (multiple checkbox options), we need to iterate through each one of it searching for the match to be checked. Here we have used the attribute ‘value’ to choose our correct option. You can use any of the available property ‘attribute-value’ pair.

Selecting a checkbox is no different than clicking on it – hence,

The alternatives

The commented code above shows some alternative ways to select a checkbox with Selenium WebDriver. You can even click on the desired checkbox option directly by identifying it using any of the methods available in Selenium WebDriver – id, name, xpath, etc.

The Result

Select a Checkbox with Selenium WebDriver | Once you run this program it will open the browser >> open the Selenium Framework Practice form URL >> Check the ‘Option 1’ checkbox.

Important Note

Working with checkbox is very simple using Webdriver. It is same as click operation. But it is always recommended to check if that is already in selected mode or deselected. Because, if it is already selected and when you click on the same element it will get deselected. This can be done using the isSelected() function.

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Leave a Reply

Your email address will not be published. Required fields are marked *