Articles → SELENIUM → Handle checkbox in Selenium

Handle checkbox in Selenium






Methods to handle checkbox









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





Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250