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 Chrome, WebDriver 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!