How To Select Multiple Checkbox in Selenium

In this Post, we will see “How To select multiple checkbox in Selenium” with examples.

Sometimes a group of checkboxes is created by using same name then it’s very difficult to select checkboxes in Selenium. However each checkboxes have different attributes values.

First we will learn How to check one checkbox in Selenium.

How to Select Only One Checkbox in Selenium:

Using click() method we can check-uncheck the checkbox in Selenium.

In below screenshot, you can see there are multiple checkbox with same name.

Checkbox in Selenium Webdriver

How to Select Multiple Checkbox in Selenium

 

//Click on Checkbox
    
driver.findElement(By.xpath("//input[@id='isAgeSelected']")).click();
//Verify checkbox is selected or not
WebElement chkbox = driver.findElement(By.xpath("//input[@id='isAgeSelected']"));
System.out.println(chkbox.isSelected());

If it return true then checkbox is selected else unselected.

isSelected() – This Method is used to verify checkbox is selected or not.

 

Complete Selenium Code :

package datapkg;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SelectCheckbox {
    
    public static void main(String[] args) {
        
        
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("https://www.seleniumeasy.com/test/basic-checkbox-demo.html");
    driver.manage().window().maximize();
    
    
    //Click on Checkbox
    
    driver.findElement(By.xpath("//input[@id='isAgeSelected']")).click();
    
        //Verify checkbox is selected or not
    WebElement chkbox = driver.findElement(By.xpath("//input[@id='isAgeSelected']"));
    
    if(chkbox.isSelected()) {
        
        System.out.println("Checkbox is ON");
    }else {
        
        
        System.out.println("Checkbox is Off");
    }
}

}

 

How to Select Multiple Checkbox in Selenium WebDriver Java:

Now we will see how to select multiple checkbox. In this case we will use input type – checkbox in xpath.

Selenium Code-

package datapkg;

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SelectCheckbox {
    
    public static void main(String[] args) {
        
        
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("https://www.seleniumeasy.com/test/basic-checkbox-demo.html");
    driver.manage().window().maximize();
    
    
    //Click on Checkbox
    
    List <WebElement> AllCheckboxes =  driver.findElements(By.xpath("//input[@type='checkbox']"));
    
    int size = AllCheckboxes.size();
    System.out.println(size);
    
    
    for(int i = 0; i<size; i++) {
        
        AllCheckboxes.get(i).click();
        
    }
}

}

Code explanation:-

  • First we stored the all checkboxes into List interface. Checkbox is a web-element that is why we stored into web-element type into List.
  • Find out the size of checkboxes (number of counts) using size() method.
  • Using For loop and get() method we iterate all checkboxes.
  • Using click() method we select the checkbox.

How to select checkbox randomly in Selenium:

Important Note: In above screenshot, we can see input tag and label of checkboxes.

If we want to select all checkboxes then no issue, we can use above locator (//input[@type=’checkbox’]) which I have mentioned in selenium code.

But if we want to select only one checkbox then we cannot use above locator (If will use this locator in that case it will select by default first checkbox of the page).

We can see in above screenshot label is different for different checkboxes. So by using label we can create a locator below-

//label[text() = ‘Option1’]     // it will select first checkbox

Same like if we want to select second checkbox then,

//label[text() = ‘Option2’]


Let’s take an example of handle dynamic checkbox.

How to handle dynamic checkbox in Selenium:

How to Check dynamic checkbox in Selenium

 

In above screenshot, we can see in html view there are multiple radio button which have value tag.

Using getAttributes() method we will get value of the radio/checkbox and click on this, no matter where it will on the web pages. If it will change the location on web pages, yet value will be same and we can easily click on this.

Selenium Code –

package datapkg;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SelectCheckbox {
    
    public static void main(String[] args) {
        
        
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://seleniumpractise.blogspot.com/2016/08/how-to-automate-radio-button-in.html");
    driver.manage().window().maximize();
    
    
    //Click on Checkbox
    
    List <WebElement> AllCheckboxes =  driver.findElements(By.xpath("//input[@type='radio']"));
    
    int size = AllCheckboxes.size();
    
    
    
    for(int i = 0; i<size; i++) {
        
        String value = AllCheckboxes.get(i).getAttribute("value");
        
        if(value.equalsIgnoreCase("PYTHON")){
            
            AllCheckboxes.get(i).click();
            break;
            
        }
        
        
    }

    
    }

}

Note: Same concept apply on Checkbox or radio buttons.

 

Troubleshooting:

Sometimes we encounter NoSuchElementException while running the selenium code. It means element is not found on the web pages. At that time we have to check below points.

  1. Check the locator again using inspect element or using firepath.
  2. Some locators are dynamic in nature. In this case, use xpath() or cssSelector() which are more reliable.
  3. Sometimes it could be wait issue. Here you can put one of the wait commands before the web element.

How to select multiple checkbox in UiPath:

In UI Path, we can select checkboxes using below steps.

To check a checkbox, set property ‘Action’ to ‘check’.

To uncheck a checkbox, set property ‘Action’ to ‘uncheck’.

Note: In case of multiple checkboxes first we need to identify at the level parent and their children of the type ‘checkbox’.

 

Watch Video:

 


Summary:

In this post, we have covered ‘How to Select Multiple Checkbox in Selenium’  and how to use it.

We have also covered – How to Select Only One Checkbox in Selenium, How to select checkbox randomly in Selenium, How to select multiple checkbox in UiPath, How to handle dynamic checkbox in Selenium.

I am sure this content added some additional value in your skills and also helpful to preparation of your interviews.

Final word, Bookmark this post “How to Select Multiple Checkbox in Selenium” for future reference.

If you have other questions or feedback, the comment section is yours. Don’t forget to leave a comment below!

 

Must Read:

How to Handle Dropdown without Select in Selenium

 

 What is Robot Class in Selenium

Leave a Comment