Canarys | IT Services

Blogs

Handling Radio buttons and Checkboxes using Selenium Webdriver

Share

The main difference between Radio button and Checkbox is that, using radio button we will be able to select only one option from the options available. whereas using checkbox, we can select multiple options.

When we inspect a Radio button or a Checkbox, HTML code looks as below

Radiobutton

Checkbox

Using Click() method in Selenium we can perform the action on the Radio button and on Checkbox.

Example:

Webelement  maleRadioBtn = driver.findElement (By.id(“gender-male”));

maleRadioBtn.click();

Before performing the click even on the Radio buttons or checkboxes we will have to verify

  • If Radio button or Checkbox is displayed on the webpage
  • If Radio button or Checkbox is enabled on the webpage
  • Check the default selection of the Radio button or Checkbox

Above mentioined verification can be done using predefined menthods in Selenium

  • isDisplayed()
  • isEnabled()
  • isSelected()

isDisplayed()

WebElement maleRadioBtn = driver.findElement (By.id(“gender-male”));

maleRadioBtn.isDisplayed // this returns a Boolean value, if it returns true then said radio button is present on the webpage or it returns False.

isEnabled()

maleRadioBtn.isEnabled() // this returns a Boolean value, if it returns true then said radio button is enabled on the webpage or it returns False

isSelected()

maleRadioBtn.isSelected() // this returns a Boolean value, if it returns true then said radio button is selected or it returns False

NOTE: The same menthods can be used while working with Checkboxes.

Below is the snippet of the code for Radio buttons

package com.explorations.scripts;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class HandlingRadioButton

{

       public static void main (String args[])

       {

              FirefoxDriver driver = new FirefoxDriver();

              driver.get(“http://sites.ecanarys.com/Nopcommercesite/”);

              driver.manage().window().maximize();

              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

             

              //Clicking on the Register link on the Home page

              driver.findElement(By.linkText(“Register”)).click();

             

              //Identifying Male radio button using its ID as an locator

              WebElement maleRadioBtn = driver.findElement(By.id(“gender-male”));

             

              //Checking if the Male Radio button is displayed on the Webpage and printing the status

              boolean radioBtnIsDisplayed = maleRadioBtn.isDisplayed();

              System.out.println(“Is Male radio button displayed: “+radioBtnIsDisplayed);

             

              //Checking if the Male Radio button is enabled on the webpage and printing the status

              boolean radioBtnIsEnabled = maleRadioBtn.isEnabled();

              System.out.println(“Is Male radio button enabled: “+radioBtnIsEnabled);

             

              //Checking the default radio button selection status

              boolean radioBtnIsSelected = maleRadioBtn.isSelected();

              System.out.println(“Default Radio button selection Status: “+radioBtnIsSelected);

             

              //Selecting male radio button

              maleRadioBtn.click();

             

              //rechecking the male radio button selection status and printing it..

              boolean radioBtnNewSelectionStatus = maleRadioBtn.isSelected();

              System.out.println(“Male radio Selection status after perform click() event: “+radioBtnNewSelectionStatus);

              driver.quit();

       }

}

Radiobutton output

 

Below is the snippet of the code for Checkboxes

package com.explorations.scripts;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class HandlingCheckbox {

       public static void main(String args[])

       {

              FirefoxDriver driver = new FirefoxDriver();

              driver.get(“http://sites.ecanarys.com/Nopcommercesite”);

              driver.manage().window().maximize();

              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

             

              //Clicking on the Register link on the Home page

              driver.findElement(By.linkText(“Register”)).click();

             

              //Identifying Terms and Condition checkbox using its ID as an locator

              WebElement tcCheckbox = driver.findElement(By.id(“accept-privacy-policy”));

             

              //Checking if the Terms and Condition checkbox is displayed on the Webpage and printing its status

              boolean tcCheckIsDisplayed = tcCheckbox.isDisplayed();

              System.out.println(“Is Terms and condition checkbox displayed: “+tcCheckIsDisplayed);

             

              //Checking if the Terms and Condition checkbox is enabled on the webpage and printing its status

              boolean tcCheckboxIsEnabled = tcCheckbox.isEnabled();

              System.out.println(“Is Terms and condition checkbox enabled: “+tcCheckboxIsEnabled);

             

              //Checking the default selection status of Terms and Condition checkbox

              boolean tcCheckboxIsSelected = tcCheckbox.isSelected();

              System.out.println(“Default Terms and Condition checkbox status: “+tcCheckboxIsSelected);

             

              //Selecting the Terms and conditions checkbox

              tcCheckbox.click();

             

              //rechecking the Terms and Condition checkbox status and priting its status

              boolean tcCheckboxNewStatus = tcCheckbox.isSelected();

              System.out.println(“Terms and Condition checkbox status after perform click() event: “+tcCheckboxNewStatus);

              driver.quit();

       }

}

Checkbox output

Leave a Reply

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

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.