Toggling a checkbox on/off through Selenium webdriver

|
| By Webner

Below is the script for on/off the checkbox using webdriver:

package mypackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class myclass
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.facebook.com";
driver.get(baseUrl);
WebElement chkFbPersist = driver.findElement(By.id("persist_box"));
for(int i=0; i<2; i++)
{
chkFbPersist.click();
System.out.println(chkFbPersist.isSelected());
}
driver.quit();
}
}

Screenshot below:

Leave a Reply

Your email address will not be published. Required fields are marked *