Friday 7 July 2017

Selenium WebDriver : Working with Checkboxes

In this post we are going to learn about automating the interaction with Checkboxes. Checkbox is an HTML Web Page element which allows users to select one or more options that are listed on a Web Page. Users just need to select the required option from the list, so we just need to click the Checkbox Option through our Automation script to select one of the Checkboxes.

Don't forget to subscribe to our YouTube Channel to see how to Automate and select a checkbox :

 YouTube Channel : Click Here



HTML Source Code for checkboxes looks like :

<input id="checkBox1" type="checkbox">
<input id="checkBox2" type="checkbox">
<input id="checkBox3" type="checkbox">  



We need to find the required checkbox(s) and click on it. Sounds simple right ?
So let's see how that can be done :


public class Sample {
   public static void main(String[] args) {
      //Initialize Browser
      System.setProperty("webdriver.gecko.driver", "Path to geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      driver.manage().window().maximize(); 
      driver.get("Web Page URL");
 
      //Find Element and Click
      WebElement element = driver.findElement("Checkbox locator");
      element.click();
   }
}



element.click() will click on the required checkbox option.

So that's the way we can click on the required checkbox option through our Automation script.

Feel free to comment in the comments section in case you have any queries.
Happy Learning.

Don't forget to Subscribe :
YouTube Channel : Click Here