Articles → SELENIUM → Handle Checkbox In Selenium

Handle Checkbox In Selenium






Methods To Handle Checkbox




  1. Click – Method used to check the checkbox (if unchecked) or uncheck the checkbox (if checked).
  2. isSelected – Method used to check if checkbox is checked or not. If checkbox is checked, then this method will return true else false.

Example




	<!DOCTYPE html>
	<html lang="en"
		xmlns="http://www.w3.org/1999/xhtml">
		<head>
			<meta charset="utf-8" />
			<title></title>
		</head>
		<body>
			<input type="checkbox" id="chkTest" checked="checked" />
		</body></html>




package com.FirstSeleniumProject;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

	public static void main(String[] args) {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		driver.findElement(By.id("chkTest")).click();

		System.out.println(driver.findElement(By.id("chkTest")).isSelected());

		driver.findElement(By.id("chkTest")).click();

		System.out.println(driver.findElement(By.id("chkTest")).isSelected());
	}

}



Output




Picture showing how the checkbox is handled in selenium
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Saturday, April 15, 2017

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250