How to Select Radio Button and CheckBox in Selenium

Radio Button in Selenium

Radio Buttons too can be toggled on by using the click() method.

Using for practice, see that toggles on the “Option1” radio button. toggles on the “Option2” radio button leaving the “Option1” unselected.

Radio Button In Selenium

How to Select Checkbox in Selenium

Toggling a check box on/off is also done using the click() method.

The code below will click on Facebook’s “Keep me logged in” check box twice and then output the result as TRUE when it is toggled on, and FALSE if it is toggled off.

Select Checkbox In Selenium

Select Checkbox In Selenium

isSelected() method is used to know whether the Checkbox is toggled on or off.

Here is another example:

Select Checkbox In Selenium

Complete Code

Here is the complete working code

import org.openqa.selenium.By;		
import org.openqa.selenium.WebDriver;		
import org.openqa.selenium.*;		

public class Form {				
    public static void main(String[] args) {									
    	// declaration and instantiation of objects/variables		
        WebDriver driver = new ChromeDriver();					

        WebElement radio1 = driver.findElement("vfb-7-1"));							
        WebElement radio2 = driver.findElement("vfb-7-2"));							
        //Radio Button1 is selected	;			
        System.out.println("Radio Button Option 1 Selected");					
        //Radio Button1 is de-selected and Radio Button2 is selected	;			
        System.out.println("Radio Button Option 2 Selected");					
        // Selecting CheckBox		
        WebElement option1 = driver.findElement("vfb-6-0"));							

        // This will Toggle the Check box 	;			

        // Check whether the Check box is toggled on 		
        if (option1.isSelected()) {					
            System.out.println("Checkbox is Toggled On");					

        } else {			
            System.out.println("Checkbox is Toggled Off");					
        //Selecting Checkbox and using isSelected Method		
        WebElement chkFBPersist = driver.findElement("persist_box"));							
        for (int i=0; i<2; i++) {											
            System.out.println("Facebook Persists Checkbox Status is -  "+chkFBPersist.isSelected());							


If you encounter NoSuchElementException() while finding elements, it means that the element is not found in the page at the point the Web driver accessed the page.

  1. Check your locator again using Firepath or Inspect Element in Chrome.
  2. Check whether the value you used in the code is different from the one for the element in Firepath now.
  3. Some properties are dynamic for few elements. In case, you find that the value is different and is changing dynamically, consider using By.xpath() or By.cssSelector() which are more reliable but complex ways.
  4. Sometimes, it could be a wait issue too i.e., the Web driver executed your code even before the page loaded completely, etc.
  5. Add a wait before findElement() using implicit or explicit waits.


  • The table below summarizes the commands to access each type of element discussed above
Element Command Description
Check Box, Radio Button click() used to toggle the element on/off