×

Selenium WebDriver- Checkbox handling

Selenium WebDriver- Checkbox handling

In this tutorial, we will be learning about handling the checkbox in selenium WebDriver.

Let us take one example to give you a better understanding of Checkbox handling, and we will create a sample test script which is as follows;

For our testing purpose, we are using the irctc website to perform Checkbox handling in selenium WebDriver.

In this test case, we will automate the following test scenarios:

Steps Action Method Used Input Excepted Result
1. Open the Google Chrome Browser. System.setProperty()   The Google Chrome browser must be opened.
2. Navigate to the website. get() https://www.irctc.co.in/nget/train-search The home page window must be displayed.
3. Identify the Journalist Concession checkbox and check that the checkbox is selected already or not. isSelected()   A Journalist concession checkbox should be identified.
4. Check whether the checkbox is clicked or not. click()   The particular checkbox should be clicked.
5. Check whether the checkbox is selected or not. isSelected()   The particular checkbox should be selected.
6. Closing the browser window. close()   The browser window should be closed.

Open the Eclipse IDE and the existing test suite new_test, which we have created in WebDriver Installation section of the WebDriver tutorial.

Then, right-click on the src folder and create a new Class File from New ? Class.

Open the Eclipse IDE
  • Give the Class name as Checkbox_handling and click onthe Finish button.
Class name as Checkbox_handling

Step1:

To launch the Google Chrome browser, we need to download the ChromeDriver.exe file and set the system property to the path of ChromeDriver.exe file.

Here is the code for setting the system property of the Google chrome Browser:

System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32 (1)\chromedriver.exe");
// create driver object for CHROME browser
WebDriver driver=new ChromeDriver(); 

Step2:

Now, we will get to the desired URL:

//navigate to the website.
 driver.get("https://www.irctc.co.in/nget/train-search"); 

Step3:

After that, we will move to our next step, where we are handling the checkbox, which is presented on the given website, and check whether the checkbox got selected or not using the isSelected() method.

Note: the isSelected() method is used to check whether the checkbox is already selected or not.

If it returns true, that means the checkbox got selected; otherwise, it returns false.

To identify the checkbox first, right-click on the journalist concession checkbox and select the Inspect element field which is as shown below,

journalist concession checkbox

The developer tool window will be launched, with all the specific codes used in the development of the particular checkbox.

development of the particular checkbox.

Here the sample code,

//identify the check box present on the web page
 System.out.println(driver.findElement(By.xpath("//label[contains(text(),'Journalist Concession')]")).isSelected()); 

Step4:

Now, we will check whether the checkbox is got clicked or not with the help of the click() method.

Note: the click() method is used to perform the left click operation on the link, checkbox, radio button.

Here the sample code,

//click on the checkbox
driver.findElement(By.xpath("//label[contains(text(),'Journalist Concession')]")).click();  
System.out.println("check box is clicked");
Thread.sleep(2000);

Step5:

After that, we will check that the checkbox got selected or not

Here the sample code,

//check whether the checkbox is selected or not
 System.out.println(driver.findElement(By.xpath("//label[contains(text(),'Journalist Concession')]")).isSelected()); 

Step6:                                                                                 

Finally, we close the browser window.

 //close the browser
 driver.close();

After completing all the given steps, our final text script will look like this:

package testpackage;
 import java.util.concurrent.TimeUnit;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.chrome.ChromeDriver;
 public class Checkbox_handling {
 public static void main(String[] args) throws InterruptedException{
 System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32 (1)\chromedriver.exe");
 // Create driver object for CHROME browser 
 WebDriver driver=new ChromeDriver();
 //maximze the window size
 driver.manage().window().maximize();
 //manage the pageload time 
 driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS);
 //delete all the cookies
 driver.manage().deleteAllCookies();
 //naviagte the the URL
 driver.get("https://www.irctc.co.in/nget/train-search");
 //identfiy the check box present on the web page
 System.out.println(driver.findElement(By.xpath("//label[contains(text(),'Journalist Concession')]")).isSelected());
 //click on the checkbox
 driver.findElement(By.xpath("//label[contains(text(),'Journalist Concession')]")).click();  
 System.out.println("check box is clicked");
 Thread.sleep(2000); 
 //check whether the checkbox is selected or not
 System.out.println(driver.findElement(By.xpath("//label[contains(text(),'Journalist Concession')]")).isSelected()); 
 //Closing browser  
 driver.close();    
 }
 } 
  • To run the test script in Eclipse, right-click on the window and then click Run as ? Java application and the test script will be launched in the chrome browser, and automate all the test scenarios.
automate all the test scenarios.

And print the result of all the commands in the eclipse console window which is as shown below,

commands in the eclipse console window

Related Topics

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 minutes read.

Selenium WebDriver-CSS Selector locator

Selenium WebDriver-CSS Selector locator A CSS selector is used to identify the web element based on their “# id” and “. Class”, Html tags, and attributes. A CSS stands for Cascading Style...

1 minute read.

CSS Selector: Sub String in Selenium IDE

CSS Selector: Sub String CSS Selector Sub-string is used to match a partial string to locate a particular web element. There are three ways to match a substring using a CSS Selector. Match...

2 minutes read.

CSS Selector: ID in Selenium IDE

CSS Selector: ID The id attribute is used to identify the web element uniquely in the web pages. Syntax of ID Attribute:  [HTML tag][#][Value of ID attribute] The hash sign (#): it is mandatory while using the...

1 minute read.

Selenium Wait

In this tutorial, we will understand how we can avoid synchronization issues while executing the test script in a selenium WebDriver. Before going further in this tutorial, first, we will understand that...

10 minutes read.

Selenium WebDriver-CSS Selector: Tag and Attribute

Selenium WebDriver-CSS Selector: Tag and Attribute We will locate the web element with the help of Tag and attribute CSS selector in this section of the tutorial. Where Tag and attribute are...

5 minutes read.

Selenium WebDriver- Checkbox handling

Selenium WebDriver- Checkbox handling In this tutorial, we will be learning about handling the checkbox in selenium WebDriver. Let us take one example to give you a better understanding of Checkbox handling,...

4 minutes read.

Selenium WebDriver- Microsoft Edge Browser

Selenium WebDriver- Microsoft Edge Browser: In this tutorial, we will learn how to launch the Microsoft Edge browser in the Selenium WebDriver. Before we start automating our test script with Microsoft Edge...

4 minutes read.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

1 minute read.

XPath Axes Selenium WebDriver

XPath Axes XPath axes are those axes that are used to search for the multiple nodes in the XML document from the current node context. These methods are mainly used when the...

14 minutes read.

Selenium WebDriver – Web Element Controls

Selenium WebDriver – Web Element Controls A web element is an interface that is implemented by a remote web element class. The Web element control can be used to perform the data...

1 minute read.

Selenium WebDriver – Window handling

Selenium WebDriver – Window handling In this tutorial, we will understand how we can handle the window, multiple windows, and pop-ups in selenium WebDriver. We have different types of window, and pop-ups...

7 minutes read.

WebDriver-Name Locator:

WebDriver-Name Locator: The name locator is used to identify any element in UI with the help of the name backend attribute. The Syntax of the name attribute is as below: driver.findElement(By.name(“value of the...

4 minutes read.

CSS Selector: ID/Class and Attribute in Selenium IDE

CSS SELECTOR: ID/Class and Attribute The Id, class, and attribute of CSS selector are used to identify the web element to access the web pages.  Syntax for Id/Class and Attribute: css= <./#> {[attribute=Value of...

2 minutes read.

WebDriver-Link Text locator

WebDriver-Link Text locator The Link text locator is used to identify only web link elements based on the value of the link. The Link text locators cannot be used for other than...

4 minutes read.

Selenium WebDriver - IE [Internet Explorer] browser

Selenium WebDriver - IE [Internet Explorer] browser In this segment, we will understand how to run a test script with the help of the IE (Internet Explorer) browser. It is a standalone...

4 minutes read.

Id Locator in Selenium IDE

Id Locator Id locator is used to find the web element by the help of id of that element because ID is likely to be unique for each web element. Target Format: Id=id of the...

2 minutes read.

Selenium WebDriver-CSS Selector: Substring Matching

CSS Selector: Substring Matching Selenium WebDriver In this tutorial, we will learn how to identify the web element using the substring method. A Sub-string contains three methods for identifying the web element...

9 minutes read.

Selenium Tool Suite

The selenium tool suite is a combination of multiple software tools, and the entire tools having a different approach to support automation testing. Selenium test tool suites are as follows- Selenium...

2 minutes read.

Selenium WebDriver Tutorial

Seleniun WebDriver Introduction Selenium WebDriver is the essential tool of Selenium Tool Suite because it can directly communicate the browser without any server.  Selenium WebDriver is a browser automation framework that accepts...

3 minutes read.