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

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


If you are using Webdriver then the item you are looking for is Selected.

Often times in the render of the checkbox doesn't actually apply the attribute checked unless specified.

So what you would look for in Selenium Webdriver is this

isChecked = e.findElement(By.tagName("input")).Selected;

As there is no Selected in WebDriver Java API, the above code should be as follows:

isChecked = e.findElement(By.tagName("input")).isSelected();


if ( !driver.findElement(By.id("idOfTheElement")).isSelected() ){     driver.findElement(By.id("idOfTheElement")).click();}


 if(checkBox.getAttribute("checked") != null) // if Checked     checkBox.click();                         //to Uncheck it 

You can also add an and statement to be sure if checked is true.