×

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 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.

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.

Drag and Drop Event Handling

Drag and Drop Event Handling: In this tutorial, we will learn how to handle the drag and drop event in selenium WebDriver using the action class. Before going further in this tutorial...

6 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-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.

Difference between Selenium WebDriver and Selenium RC

Difference between Selenium WebDriver, and Selenium RC: Selenium WebDriver and selenium RC (Remote Control) both are different in any manners which we see later in this tutorial, let us see the...

3 minutes read.

Selenium WebDriver-CSS Selector: Tag and Class

Selenium WebDriver-CSS Selector: Tag and Class We will locate the web element with the help of tag and class CSS selector in this section of the tutorials Where Tag and class are...

4 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.

Selenium WebDriver Commands

Selenium WebDriver Commands: As we learn previously in the Selenium-IDE tutorial, Selenium commands are the set of commands that are used to run a Selenium test script. But whereas, Selenium WebDriver had...

2 minutes read.

Selenium WebDriver-CSS Selector: Tag and ID

Selenium WebDriver-CSS Selector: Tag and ID We will locate the web element with the help of tag and id CSS selector in this section of the tutorials Where Tag and id are...

4 minutes 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.

Name Locator in Selenium IDE

NAME LOCATOR: It is used to find the name in the web element. If we have multiple web elements of the same name, then the first matched element would be returned. Open the...

3 minutes read.

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.

Operational Controls Methods in Selenium WebDriver

Operational Controls Methods The Operational controls are used to perform all the actions on the web elements like click on the button, pass the value in the text box, and click...

4 minutes read.

Selenium WebDriver-XPath Locator

Selenium WebDriver-XPath Locator The locators can make the object identification of all the selenium tools. There are eight locators available in selenium web driver, but most of the time we go for...

14 minutes read.

WebDriver-Tag Name Locator

Selenium WebDriver-Tag Name Locator The tag name locator is used along with the findElements() method to identify multiple similar elements in the UI. It is used to locate the web element with...

3 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.

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 Tutorial

What is Selenium? Selenium is a functional testing tool and compatible with the non-functional testing tools also. Selenium is free and does not require any licensing. Functional tests script executed by...

6 minutes read.

Selenium WebDriver- Radio button handling

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

3 minutes read.