32. Using click( ) predefined method for selecting a check box option

click( ) is a predefined method of Selenium WebDriver Class which can also be used to select Check box option.

Test Description:

Lets implement the following Test as explained using the comments on the below screenshot:

Lets Implement This:


1. Create a new Java Project say 'WebDriver-Project6' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package6' under the newly created project.
4. Create a Java Class file say 'Class6' under the newly created package as shown below:

Actual Steps:

1. Write the following code into the newly created Java Class file as shown below and make sure that you resolve all the errors before going to next step:

2. The above code will create a Selenium WebDriver object, Launch a new Firefox Browser session, opens the http://book.theautomatedtester.com/chapter1 page in the Browser window and finally it maximizes the Browser window as shown below:

3. Now lets write Selenium WebDriver code for selecting the check box option in chapter1 page as explained in the above Test Description by following the below steps:

4. Using FireBug options, Inspect the check box and identify the locator after observing the below code in the below screenshot:

5. After seeing the above html code in the screenshot, its very clear that we've to identify the check box button using NAME locator i.e. using name=selected(1234)  in this case.

6. Also we've to use the above NAME locator in the Selenium WebDriver Command which is used to select the specified check  box. _driver.findElement(By.name("Name LOCATOR")).click();  is the syntax we've to use for selecting the specified locator.

Lets understand the _driver.findElement(By.name("Name LOCATOR")).click();   syntax by breaking it as below:

  • _driver - is the WebDriver object
  • findElement( )  - is used for locating the elements on which we have to perform the operations
  • By.name("Name LOCATOR") By.name informs the findElement( ) to find the elements using Name LOCATOR
  • Locator - is the actual locator we want to find (i.e. Check box option  in this case )
  • click( ) - is the WebDriver command which can also be used for selecting the check box option

7. So after understanding the things in step5 and step6, its very clear that we've to write _driver.findElement(By.name("selected(1234)").click(); to select the check box option as shown below:

8. Run the Test using JUnit Test as shown below:

9. After the test has run completely, observe that the check  box option got selected as shown below:

Watch the below video:

Click here to watch the video.

Download this Project:

Click here to download this project and import into Eclipse IDE  on your machine.

Please comment below to feedback or ask questions.

Using sendKeys( ) predefined method to select a list item from the drop down list will be explained in the next post.