Writing Automation Script for Check Box and Radio Buttons

How to handle a checkbox and radio button on a webpage

Handling Check boxes and Radio buttons using Selenium webdriver

In this tutorial, we are going to learn about – How to handle a checkbox and radio button on a webpage. We are going to use Selenium and Java for writing code in this tutorial.

This tutorial is part of Complete Selenium 3.0 Tutorials, being developed by Techcanvass.

We are going to cover the following topics:

  • Methods to interact with Check boxes
  • Methods to interact with Radio Buttons
  • Writing the test cases for both

Locating Check boxes and Radio Buttons

We can locate the check boxes and radio buttons on a web page using any of the locators or as per the locators usage strategy for the project.

You can refer to the detailed post on locators in our tutorial series.

Basics of Locators Selenium

Read Also: Basics of Locators in Selenium

Interacting with Check boxes and Radio buttons

Click() Method

Click method is used to select a value in checkbox or radio buttons. Once we locate the radio button or check box, we can use the click() method to select a value.

driver.findElement(By.id(“checkbox1”)).click();

IsSelected()

isSelected() method is used to ascertain if the check box or radio button is already selected? The example code written below shows the usage:

if ( !driver.findElement(By.id(“checkbox1”)).isSelected() )

{

driver.findElement(By.id(“checkbox1”)).click();

}

In the above code, we first check if the checkbox is selected, if not, then we select the check box.

Example for this tutorial

We are going to use http://techcanvass.com/examples/register.html for this example. This is a simple web page to register early for buying a smartphone to avail heavy discounts.

Register Page for Selenium

Writing Script for Radio Button

If you inspect the code for radio button, you will realize that the names for both the radio buttons is the same as “gender”, only the values differ. This is possible if IDs are not provided for the radio buttons or bad HTML coding practice.

Using XPath as the locator strategy, will serve well. Use Firepath tool to get the locators for both the radio buttons and create web elements as shown below:

WebElement radio1 = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[2]/input[1]”));
WebElement radio2 = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[2]/input[2]”));

Writing Script for Check Box

We can use the XPath strategy for check boxes also.

Complete code

In our case, we will be using the following test data to submit the registration for the smart phone:

First Name: Meenaxi

Last Name: Kabra

Gender: Female

Model: Mega 123 large screen (Default value so we will not change)

Should it be delivered to your house: YES

 

Below is the complete code for the web page, we have not entered anything for the address fields.

package basicSeleniumScripts;
import org.openqa.selenium.By;

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

public class CheckboxRadiobutton {

public static void main(String[] args) throws InterruptedException {

//Don’t forget to change the path of Chromedriver as per the location on your machine

System.setProperty(“webdriver.chrome.driver”, “E:\\Selenium 3\\chromedriver.exe”);
WebDriver driver;
driver = new ChromeDriver();

driver.get(“http://techcanvass.com/examples/register.html”);

WebElement TxtFname = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[1]/input[1]”));

WebElement TxtLname = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[1]/input[2]”));

WebElement radio1 = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[2]/input[1]”));
WebElement radio2 = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[2]/input[2]”));

TxtFname.sendKeys(“Meenaxi”);
TxtLname.sendKeys(“Kabra”);

if ( radio1.isSelected())
{
Thread.sleep(5000);
radio2.click();
Thread.sleep(10000);

}

if ( radio2.isSelected())
{
radio1.click();
Thread.sleep(10000);

}

WebElement Checkbx = driver.findElement(By.xpath(“.//*[@id=’login’]/form/fieldset/p[6]/input”));

Checkbx.click();
Thread.sleep(10000);

driver.quit();

}

}

Please note that we have used Thread.Sleep() only to demonstrate that values have been entered into the fields.

What’s Next?

In this tutorial, we explained How to handle a checkbox and radio button on a webpage. You can go and read more Selenium tutorials published on this blog.

Are you looking to become an Automation tester?

Techcanvass offers Automation Testing and Business Analysis certification courses. We are an IIBA endorsed education provider (EEP), iSQI Germany Authorized Training as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

One of the key features of our courses is the Job readiness package, as shown below:

Selenium-Certification-training-with-Job-readiness