• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / How to Automate Radio button and Checkbox in Selenium webdriver

How to Automate Radio button and Checkbox in Selenium webdriver

April 23, 2015 by Mukesh Otwani 24 Comments

Radio button and Checkbox in Selenium

Hello Guys, Welcome to Selenium tutorial in this post we will see how to automate radio button and checkbox in selenium

Before moving to radio button and checkbox, you should be familiar with HTML as well.

Before moving to Checkbox please refer Basic Selenium Tutorial

HTML For Checkbox

<input type=”checkbox”>

 

For Radio Button

<input type=”radio”>

 

automate radio button and checkbox in selenium
Radio button and Checkbox in Selenium

YouTube vide for Handle Dynamic radio button and Checkbox

 

 

Automate radio button and checkbox in selenium

When you inspect these elements via firebug and firepath you will get above html type.

The main difference between radio button and checkbox is checkbox you can select multiple but for radio button, only one selection is possible.

In Selenium we have 1 method called click() to perform click events.

This click() method you can apply with radio button, checkbox, links and sometime with dropdown as well.

Let us get started

Demo Code

WebElement ele=driver.findElement(By.id());
ele.click();

In this example I have used id only but if you want to make you script stable then you should use Xpath and CSS in your script.

Before performing click action, sometimes we need to verify some activity as well, take some example

  • You need to verify whether radio button or checkbox is enabled.
  • You need to verify whether radio button or checkbox is Displayed on UI or not.
  • You need to verify whether checkbox and radio button is default selected or not.

Above validations are must used in script because automation is all about validation only. You will get these type of questions in interviews also.

 

 

These words looks quite big while listening but we can easily verify this using some predefined method in Selenium.

These methods are

isDisplayed();

isEnabled();

isSelected();

Therefore, you must be eager now how to use these methods in script so let us see these methods using a single program.

 

Program to Automate radio button and checkbox in selenium

 import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class FacebookDropdown {

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

          WebDriver driver=new FirefoxDriver();

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

          driver.get("http://www.facebook.com");

         WebElement male_radio_button=driver.findElement(By.id("u_0_e"));

         boolean status=male_radio_button.isDisplayed();

         System.out.println("Male radio button is Displayed >>"+status);

          boolean enabled_status=male_radio_button.isEnabled();

          System.out.println("Male radio button is Enabled >>"+enabled_status);

        boolean selected_status=male_radio_button.isSelected();

          System.out.println("Male radio button is Selected >>"+selected_status);

          male_radio_button.click();

        boolean selected_status_new=male_radio_button.isSelected();

          System.out.println("Male radio button is Selected >>"+selected_status_new);

     }

}

 Explanation- If you notice above scenario before click Selected status was false but after click status changed to TRUE.

 Check below image for output.

automate radio button and checkbox in selenium
Radio button and Checkbox in Selenium

 

Once you get the status you can easily verify using Assert in TestNG.

Above example will work for Checkbox as well. Please try from your side and let me know if you help required.

Now if you are comfortable with Basic Selenium, you can check out Advance Selenium Tutorial as well. 🙂

 

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

Filed Under: Basic Selenium Tagged With: automate radio button and checkbox in selenium, Click

Reader Interactions

Comments

  1. Shaheen Akhtar says

    August 2, 2019 at 12:27 AM

    How to store multiple radio buttons in properties.config file and how to validate a particular radio button value..suppose there are 3 radio buttons on properties.config file and i have to click only 1 radio button then how can i do that?
    i have to handle multiple radio button on the basis of selection of radio buttons by using properties.config file.

    Reply
    • Mukesh Otwani says

      August 3, 2019 at 10:13 AM

      Hi Shaheen,

      You can use indexing based on xpath selected like [1] and [2]…so on. Then as per your scenario, choose corresponding xpath from .properties file and proceed further

      Reply
  2. Ravi says

    January 27, 2017 at 11:56 AM

    Hi Sir,
    I am not able to handle the radiobutton which is on Label.Can you please tell of an element is in label how to handle the same

    Reply
    • Mukesh Otwani says

      January 27, 2017 at 4:11 PM

      Hi Ravi,

      Try to click on tag which should be on adjacent to label tag. It could be either parent or child tag.

      Reply
  3. Vinay says

    September 26, 2016 at 1:24 PM

    It is not working for chrome browser in Run mode. If I have to used with debug, it is working

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 11:05 AM

      Hi Vinay,

      Ideally it should happen. Make sure you are using 2.24 version of Chrome driver

      Reply
  4. Raj says

    September 24, 2016 at 12:58 PM

    Hi Mukhesh,
    I’m trying to automate google form page.
    I’m unable to select “Mobile Phone” and “Gambia+220” option.
    Through Robot class it will be difficult I suppose as there are >100 countries in the list.
    Could you please help me how to achieve this ? Thanks in advance

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 2:19 PM

      Kindly share the URL itself.

      Reply
  5. sudhr says

    September 21, 2016 at 6:18 AM

    can u give me the link of hidden webelement extension video( second part of the video for dynamic radio button selection. )

    Reply
    • Mukesh Otwani says

      September 23, 2016 at 11:32 PM

      Reply
      • sudhr says

        September 30, 2016 at 7:00 AM

        u r awesome mukesh. i learn a lot from u r blog…

        Reply
        • Mukesh Otwani says

          October 3, 2016 at 4:32 PM

          Welcome boss

          Reply
  6. Pooja Vengurlekar says

    September 14, 2016 at 4:27 PM

    Can you please give me your Email ID?

    Reply
    • Mukesh Otwani says

      September 15, 2016 at 4:34 PM

      mukeshotwani@learn-automation.com

      Reply
  7. Ashwini Swami says

    September 1, 2016 at 11:41 PM

    Error : “The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)”

    plz provide solution

    Reply
    • Mukesh Otwani says

      September 2, 2016 at 5:44 PM

      Hi Ashwini Kindly follow below steps http://learn-automation.com/solution-for-sendkeyscharsequence-in-selenium/

      Reply
  8. Mun says

    August 17, 2016 at 12:11 PM

    WOOW that is great .. I was very effective for me specially boolean value and i added extra condition , fortunately its working.

    thanks for you kind effort.

    Reply
    • Mukesh Otwani says

      August 18, 2016 at 9:34 AM

      Cheers Mun 🙂

      Reply
  9. Ankita Gupta says

    August 10, 2016 at 5:14 PM

    Hi Mukesh,

    Apologies to writing query related to handling of drop down without select web element in this section because I could not able to write my post there as it was taking time to load the comment section.

    Can you please help me how to write code for handling the drop down without select webelement using div ideally.

    I have tried with the following code as:
    List message_list=driver.findElements(By.xpath(“//div[@id=’the-message-cloud’]”));
    for(WebElement element: message_list)
    {
    System.out.println(“list name::”+element);

    }
    but it is giving unexpected result:

    list name::[[FirefoxDriver: firefox on WINDOWS (77893ede-a2b2-4bc8-b152-ec71a258ab57)] -> xpath: //div[@id=’the-message-cloud’]]
    PASSED: verifyApplication

    please find below HTML code:
    The Message Cloud

    Smart Personalization Email

    Smart Personalization Web

    Reply
    • Mukesh Otwani says

      August 11, 2016 at 1:01 AM

      Hi Ankita,

      try below code

      List message_list=driver.findElements(By.xpath(“//div[@id=’the-message-cloud’]”));
      for(WebElement element: message_list)
      {
      System.out.println(“list name::”+element.getAttribute(“innerHTML”));

      }

      Reply
  10. Prachee Jain says

    June 2, 2016 at 2:48 PM

    Hi Mukesh,

    for selecting Marital Status for website”http://demoqa.com/registration/ ” by Xpath with chrome
    i am getting below error

    org.openqa.selenium.InvalidElementStateException: invalid element state: Element must be user-editable in order to clear it.
    (Session info: chrome=50.0.2661.102)
    (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 25 milliseconds
    i have checked Xpath is unique.

    Reply
    • Mukesh Otwani says

      June 4, 2016 at 12:41 AM

      Hi Prachee,

      try this http://learn-automation.com/how-to-solve-stale-element-reference-exception-in-selenium-webdriver/

      Reply
  11. Gayathri says

    February 12, 2016 at 1:05 PM

    please suggest one application with which i can practice more radio buttons and check boxes.

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 4:15 PM

      Hi Gayathri,

      Please try with http://demoqa.com/registration/

      Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?