How do you tell if a checkbox is selected in Selenium for Java?

Way1:

We can see whether a checkbox is checked or not using the asserted method present in selenium.

driver.findElement(By.id("locator")).isSelected() 

In case, if you want to check a checkbox if it is not selected when you can use the below code.

if ( ! driver.findElement(locator).isSelected() )
{
     driver.findElement(locator).click();
}
Way2:

If you don't get the correct answer from the Evoque or then you can find the right attribute from the HTML code, then get the attribute.

if ( driver.findElement(locator).getAttribute("checked") == null)
{
     driver.findElement(locator).click();
}
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions