
In this tutorial we will see how to handle check box in selenium webdriver.
When you try to work on check box first you need to understand you can opt multiple options. Let’s understand how check box looks like in HTML. Check below snapshot.

When html page is designed with type as “checkbox” then check box will be created it will be reflected as multiple options in a web page. Please look below snapshot.

In selenium webdriver you can handle check box using click method. Let’s see step wise
Step 1: Inspect the element and find the unique element (Xpath).
Step 2: Find the element and use click method.
Step 3: Validate the whether you have clicked or not using automation.
Step 1: Inspect the element and find the unique element (Xpath).
Xpath: //input[@name=’working’]
After finding Xpath you need to make sure 3 things whether you have written valid Xpath, Element is highlighted and having unique (1 of 1 node in html). Please look below screenshot.

Step 2: Find the element and use click method.
1 2 3 4 5 6 |
//Find element and assign in webelement WebElement workingCheckBox=driver.findElement(By.xpath("//input[@name='working']")); //clicking the element workingCheckBox.click(); |
In above expression click is method to handle check box using selenium webdriver.
Step 3: Validate the whether you have clicked or not using automation.
This is main thing we need to consider for every interview or real time as we need to cross check whether we have clicked or not? In Selenium Webdriver there are three methods by using these methods we can validate.
Validating Methods:

All above methods will return Boolean. Either true or false if it returns ‘true’ we can say that element is clicked else not clicked.
Please check below code snippet for validation part:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//validating check box is clicked or not? boolean status = workingCheckBox.isSelected(); //confirming what is there in status for learning purpose otherwise no need System.out.println("Checking boolean status:"+status); //checking condition is true or not. if(status) { System.out.println("Working check box is selected sucessfully at profession."); } else { System.out.println("Working check box is not clicked at profession."); } |
Based on Boolean status and if condition we can validate easily. Please practice once on this automation practice page where you have radio button.
Please check entire program on how to handle radio button in selenium webdriver below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
public class HandlingCheckBox { @Test public void HandlingCheckBox() { //Initialize reference to driver WebDriver driver; //Setting the properties for chromedriver System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe"); //Initialize the chromedriver for webdriver reference driver=new ChromeDriver(); //Launching application driver.get("https://automationexplore.com/selenium-automation-practice-page/"); //Setting implicit wait to load all web elements in application driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); //Maximize the window driver.manage().window().maximize(); //Find element and assign in webelement WebElement workingCheckBox=driver.findElement(By.xpath("//input[@name='working']")); //clicking the element workingCheckBox.click(); //validating check box is clicked or not? boolean status = workingCheckBox.isSelected(); //confirming what is there in status for learning purpose otherwise no need System.out.println("Checking boolean status:"+status); //checking condition is true or not. if(status) { System.out.println("Working check box is selected sucessfully at profession."); } else { System.out.println("Working check box is not clicked at profession."); } } } |
Output:

Note: To run above program you need to download browser driver as I have downloaded chrome. You can download and work based on your desired web browser driver or you can click here to download from selenium website.