How to Automate Radio button and Checkbox in Selenium webdriver

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

 

 

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

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

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

 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

22 Comments

  1. Ravi
  2. Vinay
  3. Raj
  4. sudhr
  5. Pooja Vengurlekar
  6. Ashwini Swami
  7. Mun
  8. Ankita Gupta
  9. Prachee Jain
  10. Gayathri