Table of contents
1.
Introduction
2.
Handling CheckBoxes
3.
Program
3.1.
Java Program for Custom Path
3.2.
Output
3.3.
Java Program for Handling it Dynamically
3.4.
Output
3.5.
HTML page
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Handling CheckBoxes

Author Aman Thakur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The article assumes that you are familiar with CheckBox and the installation of Selenium webdriver. If not, you can check out the article related to the article on Intoduction to Selenium webdriver and Radio buttons in HTML.
 

Enough of Introduction let see it in action.

Handling CheckBoxes

This section primarily discusses handling of radio buttons with the help of Selenium Web Driver.

There are two ways of handling CheckBoxes:

1. By using custom path: Custom path basically means when you explicitly define which radio button you wish to see changes.
2. By handling the radio button dynamically: Dynamically basically means if you define some logic how to access the radio button might be even or odd or some other pattern.


Below are the common steps which need to be followed for writing down the java code before proceeding with any of the above mentioned two steps.

Step 1: Invoke the chrome driver
Step 2: Create an HTML page
Step 3: Use any one of the two ways as you wish.

Program

The section includes the java code and html code which are needed to implement the automation testing using selenium.

Java Program for Custom Path

package RadioButtons;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class RadioButtons {
   public static void main(String[] args) {
       //Setting up webdriver path
       System.setProperty("webdriver.chrome.driver", "../Dependencies/chromedriver");


       // initializing webdriver
       WebDriver webDriver = new ChromeDriver();


       String path = "file:///home/aman-thakur/Desktop/MyDocuments/Coding%20Ninjas/Project/WebDriver/SeleniumWebDriver/src/CheckBox/index.html";
       webDriver.get(path);


       webDriver.findElement(By.xpath("//input[@value='orange']")).click();
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

Java Program for Handling it Dynamically

package RadioButtons;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class RadioButtons {
   public static void main(String[] args) {
       System.setProperty("webdriver.chrome.driver", "../Dependencies/chromedriver");


       WebDriver webDriver = new ChromeDriver();


     // replace it with your own html path
       String path = "file:///home/aman-thakur/Desktop/MyDocuments/Coding%20Ninjas/Project/WebDriver/SeleniumWebDriver/src/CheckBox/index.html";
       webDriver.get(path);


       int listSize = webDriver.findElements(By.xpath("//input [@name='fruit']")).size();


       for (int fruit = 1; fruit <= listSize; fruit++){
           if (fruit % 2 == 1) {
               webDriver.findElements(By.xpath("//input [@name='fruit']")).get(fruit - 1).click();
           }
       }
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

HTML page

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>CheckBox</title>
</head>
<body>
   <input type="checkbox" name="fruit" value="mango" /> Mango <br>
   <input type="checkbox" name="fruit" value="orange" /> Orange <br>
   <input type="checkbox" name="fruit" value="jack-fruit" /> Jack Fruit <br>
</body>
</html>

Frequently Asked Questions

1. How does xpath select the checkbox in Selenium?
Ans: xpath("//input [@name='groupname']")).size();
The above piece of code counts how many checkbox with the name group1 there are. Now we'll use the index of a certain radio button to manage the radio buttons.

2. What is a stale element exception?
Ans: An old or no longer accessible element is referred to as a stale element. Assume there is a WebElement in WebDriver that is discovered on a web page. The WebElement becomes stale when the DOM changes. The StaleElementReferenceException is thrown when we try to interact with a staled element.

3. What is Jenkins in Selenium?
Ans: Jenkins is an open-source Continuous Integration (CI) server that automates the web application creation and deployment process. You may automate testing as part of the build process by running your Selenium test suite in Jenkins.

4. What are Selenium listeners?
Ans: Listeners are those who have the ability to pay attention to a certain event. It is described as a user interface that adjusts the system's behaviour. Reports and logs can be customised using listeners.We need to know the different sorts of listeners in Selenium to obtain a better understanding of how they function. 

Conclusion

If you have reached till here that means, you really enjoyed this article. This article covers the usage for Selenium Web Driver for handling Checkbox, which are explained via code snippets and their use cases. And a program which is automated through a java program.

You might be interested in a few articles, such as Running Selenium Tests on ChromeWebDriver Commands and  Radio buttons in HTML.. Do upvote our blog to help other ninjas grow, and head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more.

Happy Learning!

Live masterclass