How to select checkboxes using selenium java webdriver?


We can select the checkbox with Selenium. In an html document, each checkbox has an attribute type set to a value as checkbox. In order to select a checkbox, we shall first identify a checkbox with any locator and then apply the click() method to it.

Example

Code Implementation.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class CheckBoxSelect{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url ="https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // identify element
      WebElement l=driver.findElement(By.xpath("//*[@value='Manual Tester']"));
      // select checkbox with click()
      l.click();
      driver.quit();
   }
}

Output

Updated on: 28-Aug-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements