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”>
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.
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
Shaheen Akhtar says
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.
Mukesh Otwani says
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
Ravi says
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
Mukesh Otwani says
Hi Ravi,
Try to click on tag which should be on adjacent to label tag. It could be either parent or child tag.
Vinay says
It is not working for chrome browser in Run mode. If I have to used with debug, it is working
Mukesh Otwani says
Hi Vinay,
Ideally it should happen. Make sure you are using 2.24 version of Chrome driver
Raj says
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
Mukesh Otwani says
Kindly share the URL itself.
sudhr says
can u give me the link of hidden webelement extension video( second part of the video for dynamic radio button selection. )
Mukesh Otwani says
sudhr says
u r awesome mukesh. i learn a lot from u r blog…
Mukesh Otwani says
Welcome boss
Pooja Vengurlekar says
Can you please give me your Email ID?
Mukesh Otwani says
mukeshotwani@learn-automation.com
Ashwini Swami says
Error : “The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)”
plz provide solution
Mukesh Otwani says
Hi Ashwini Kindly follow below steps http://learn-automation.com/solution-for-sendkeyscharsequence-in-selenium/
Mun says
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.
Mukesh Otwani says
Cheers Mun 🙂
Ankita Gupta says
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
Mukesh Otwani says
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”));
}
Prachee Jain says
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.
Mukesh Otwani says
Hi Prachee,
try this http://learn-automation.com/how-to-solve-stale-element-reference-exception-in-selenium-webdriver/
Gayathri says
please suggest one application with which i can practice more radio buttons and check boxes.
Mukesh Otwani says
Hi Gayathri,
Please try with http://demoqa.com/registration/