How to handle radio buttons and checkbox in selenium web driver

Last updated on Dec 02 2021
Swaminathan M

Table of Contents

How to handle radio buttons and checkbox in selenium web driver

In this blog, you’ll find out how to handle radio buttons in selenium web driver.

Following are the steps to handle the radio buttons:

Step 1: Invoke the Google Chrome browser.
The code to invoke a Google chrome browser is given below:

1. package mypack;
2. import org.openqa.selenium.WebDriver;
3. import org.openqa.selenium.chrome.ChromeDriver;
4. public class Class1
5. {
6. public static void main(String[] args)
7. {
8.
9. System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");
10. WebDriver driver = new ChromeDriver();
11.
12. }
13.
14. }

Step 2: The second step is to navigate to the web site during which we’d like to handle the radio buttons.
I created the html file which contains the radio buttons. The code is given below:

1. <html>
2. <head>
3. </head>
4. <body>
5. <input type="radio" name="group1" value="Mango">Mango
6. <input type="radio" name="group1" value="Watermelon">Mango
7. <input type="radio" name="group1" value="Banana">Mango
8.
9. <input type="radio" name="group2" value="Ladyfinger">Ladyfinger
10. <input type="radio" name="group2" value="Potato">Potato
11. <input type="radio" name="group2" value="Tomato">Tomato
12. </body>
13. </html>
The code for navigating to the above html file is given below:
1. package mypack;
2. import org.openqa.selenium.WebDriver;
3. import org.openqa.selenium.chrome.ChromeDriver;
4.
5. public class Class1 {
6. public static void main(String[] args) {
7.
8. System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");
9. WebDriver driver = new ChromeDriver();
10. driver.get("file:///C:/Users/admin/Desktop/radio.html");
11. }
12.
13. 

}

The output of the above code:

handle 1
handleradio

Step 3: Select the choice Banana. we’ll locate the Banana radio button by inspecting its HTML codes.
There are two ways of handling the radio buttons:
• By using the customized path:
The code shown below handles the radio button by using the customized path.

1. package mypack;
2. import org.openqa.selenium.By;
3. import org.openqa.selenium.WebDriver;
4. import org.openqa.selenium.chrome.ChromeDriver;
5.
6. public class Class1 {
7. public static void main(String[] args) {
8.
9. System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");
10. WebDriver driver = new ChromeDriver();
11. driver.get("file:///C:/Users/admin/Desktop/radio.html");
12. driver.findElement(By.xpath("//input[@value='Banana']")).click();
13. }
14.
15. 
}

In the above, we use custom Xpath. Radio buttons contain a singular attribute, i.e., value, so we use the worth attribute to handle the radio button.
Output

handle 2
output
  • By handling the radio buttons dynamically.
  • We will first calculate the amount of radio buttons. the subsequent is that the line of code which calculates the amount of radio buttons.
int a = driver.findElements(By.xpath("//input [@name='group1']")).size();

The above line of code calculates the amount of radio buttons whose name is group1.

  • Now, we’ll handle the radio buttons by using the index of a specific radio button.

driver.findElements(By.xpath(“//input[@name=’group1′]”)).get(2).click();
Source code

1. package mypack;
2. import org.openqa.selenium.By;
3. import org.openqa.selenium.WebDriver;
4. import org.openqa.selenium.chrome.ChromeDriver;
5.
6. public class Class1 {
7. public static void main(String[] args) {
8.
9. System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");
10. WebDriver driver = new ChromeDriver();
11. driver.get("file:///C:/Users/admin/Desktop/radio.html");
12. int a = driver.findElements(By.xpath("//input [@name='group1']")).size();
13. System.out.println(a);
14. for(int i=1;i

OUTPUT

handle 3
output

Handling Checkbox

In this blog section, you’ll find out how to handle checkbox in selenium webdriver.
Let’s create a test suit during which we’ll automate the subsequent scenarios:
• Invoke a Google chrome browser.
• Navigate to the web site during which you handle the checkbox.
• Select the ‘Senior Citizen’ checkbox from the spicejet website.
• Close the driving force .
Now, we’ll create a test suit step by step so as to supply you proper understanding of the way to handle checkbox.
Step 1: Launch the Eclipse IDE.
Step 2: Right click on the src folder then click on the New > class.

handle 4
New class.

• Enter the category name. I provide the category name as Checkbox_test.

handle 5
Checkbox_test

Step 3: Now, we’ll invoke the Google Chrome browser. we’ll download the chromedriver.exe file and set the system property to the trail of your chromedriver.exe file.
Here is that the sample code to line the system property to the trail of a chromedriver.exe file.

1. System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe);
Here is that the sample code to invoke the Google chrome browser.
1. WebDriver driver = new ChromeDriver();
Combining both of the above code blocks, we'll get the code snippet to launch Google chrome browser.
1. // Set the system property
2. System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe);
3.
4. // Launch the Google Chrome browser.
5. WebDriver driver = new ChromeDriver();

Step 4: We are through with the primary test suit , i.e., invoking a Google chrome browser. Now we’ll write the code to automate the test suit . Our second test suit is to navigate to the “spicejet” website.
Here is that the sample code to navigate to the “spicejet” website.

1. driver.navigate().to("https://www.spicejet.com/");
Here is that the complete code:
1. package mypack;
2.
3. import org.openqa.selenium.WebDriver;
4. import org.openqa.selenium.chrome.ChromeDriver;
5.
6. public class Checkbox_test {
7.
8. public static void main(String[] args) {
9. // TODO Auto-generated method stub
10. System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
11. WebDriver driver = new ChromeDriver();
12. driver.navigate().to("https://www.spicejet.com/");
13.
14. }}

Step 5: Now we attempt to locate the ‘Senior Citizen’ checkbox by inspecting its HTML code.

handle 6
locate

Note the id attribute of a checkbox.

handle 7
id

In the above case, we observe that ‘id’ may be a unique attribute, so we locate the checkbox by using an id attribute.
Step 6: To automate the third test suit , we’d like to write down the code which will locate ‘Senior Citizen’ checkbox.

Here is that the code which will handle the “Senior Citizen” checkbox.

1. package mypack;
2. import org.openqa.selenium.By;
3. import org.openqa.selenium.WebDriver;
4. import org.openqa.selenium.chrome.ChromeDriver;
5. public class Checkbox_test
6. {
7.
8. public static void main(String[] args)
9. {
10. // TODO Auto-generated method stub
11. System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
12. WebDriver driver = new ChromeDriver();
13. driver.navigate().to("https://www.spicejet.com/");
14. System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
15. driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).click();
16. System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
17.
18. driver.close();
19. }
20.
21. 
}

In the above code, we’ve not used the entire ‘id’ attribute value because it is extremely big. I even have used the half a part of the ‘Senior Citizen’ checkbox, and spouse part is represented within the sort of regular expression, i.e., ‘*=’.
We have used two methods within the above code:
• isSelected(): This method determines whether the checkbox is chosen or not. If the checkbox is chosen , then this method returns true otherwise false.
• click(): This method selects the locator. during this case, it’s selecting the “Senior Citizen” checkbox.
Output

handle 8
Output

Output on the Console

handle 9
ConsoleOutput

So, this brings us to the end of blog. This Tecklearn ‘How to handle radio buttons and checkbox in Selenium web driver’ helps you with commonly asked questions if you are looking out for a job in Selenium and Automation Testing. If you wish to learn Selenium and build a career in Automation Testing domain, then check out our interactive, Selenium Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Selenium Certification Training

Selenium Certification Training

About the Course

Tecklearn’s Selenium Certification Training enables you to master the complete Selenium suite. The Selenium Training is designed to train developers and manual testers to learn how to automate web applications with a robust framework, and integrate it within the DevOps processes of an organization. This Selenium Certification Training will also help you master important concepts such as TestNG, Selenium IDE, Selenium Grid, Selenium WebDriver, etc. Get hands-on experience on widely used automation frameworks such as Data-Driven Framework, Keyword-Driven Framework, Hybrid Framework, and Behavior Driven Development (BDD) Framework. Throughout this online Instructor-led Selenium Certification Training, you will be working on real-life industry use cases.

Why Should you take Selenium Certification Training?

• The average salary of a Selenium Test Automation Engineer is $94k per year – Indeed.com.
• Automation Testing Market is expected to grow at a Compound Annual Growth Rate (CAGR) of 18.0% in the next three years.
• Global software testing market to reach $50 billion by 2020 – NASSCOM. Selenium tool supports more browsers and languages than any other testing tool.

What you will Learn in this Course?

Getting started with Selenium

• Introduction to Selenium testing
• Significance of automation testing
• Comparison of Manual and Automation Testing
• Installation of Java JDK, JRE and Eclipse

Setting the environment in Eclipse for Selenium

• Java Introduction
• Creating a Java function and executing
• Concepts of Java
• Properties File
• Reading Data from Excel File
• Database Connection
• Hands On

Advantages of Selenium automation testing

• Selenium Features
• Concept of Selenium Integrated Development Environment
• Understanding of the Selenium IDE features
• Addition of Script assertions and general commands
• Deploying the first Selenium Script
• Sample project IDE
• Recording Selenium test case
• Hands On

Selenium Web driver Automation

• Architecture of Selenium Web Driver
• Download and installation
• Creating a Java function using Selenium and execution
• Hands On

Deploying Web Drivers for scripting

• Getting the HTML source of Web Element
• Table and Form Elements
• Firebug extension and Fire Path installation
• Advance User Interactions and Cross Browser Testing
• Hands On

Deep dive into Selenium Web Driver

• Action Commands
• Web Table / Date Picker
• How to Implement Switching Commands in WebDriver
• Alerts
• Frames
• Hands On

Switching Operations in WebDriver using Window

• Selenium Webdriver Wait
• Implicit wait, Explicit wait
• Deploying searching elements using the link text, name, using XPath
• Calendar
• Hands On

Introduction to TestNG Framework

• Introduction to TestNG
• Advantages of TestNG
• Installing TestNG on Eclipse
• Rules to write TestNG
• TestNG Features
• Annotations
• Grouping
• Sequencing: Prioritization and Dependency
• Enable/Disable a test case
• Parameterization: Using Xml file and DataProvider
• Parallel Testing & Cross Browser Testing
• TestNG Report: HTML Report, Console Report, XML Report

JUnit Operations and Test Framework

• Annotations, Methods in JUnit
• Junit Test Suites, ANT Build and JUNIT reporting
• Types of Test Automation Framework
• Module Based Testing Framework
• Data Driven Testing Framework
• Keyword Driven Testing Framework
• Hybrid Driven Testing Framework
• How to implement Testing Framework in Project

Object Repository

• Understanding of Object Repository
• Learning sample scripts using object repository
• Page Object Modelling
• Page Factory

JavaScript Functions

• Autosuggestion
• Headless Browser
• Sikuli
• XPath

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "How to handle radio buttons and checkbox in selenium web driver"

Leave a Message

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