About this lesson
Select a checkbox in Selenium Java with click() and verify it with isSelected()
Selenium Java AssignmentsIntermediateCheckboxes & Radio Buttons
How it works
A checkbox has no "select" method in WebDriver. You tick it the way a user does, with click(), and you read its state with isSelected(), which returns true while the box is ticked. Reading state before and after is what turns a click into a test: the click alone proves nothing.
click() toggles. Clicking an already ticked box unticks it, so code that must leave a box ticked should check isSelected() first and click only when it returns false. Clicking the box's label toggles it too, because in the main rows of the Checkboxes page each label is tied to its input with a for attribute.
The Checkboxes page has two ordinary rows, a disabled row, a hidden row, a row with one box ticked from the start, and a nested tree at the bottom. They are all in the DOM, but only some can be clicked.
About the assignment
The first box in the ordinary row, firstSelect1, starts unticked. You read its state, tick it with a click and read the state again, so the two printed lines show the change the click caused.
What you will do
- Find the checkbox by its id.
- Print
isSelected()after the labelBefore click:. - Click the checkbox once.
- Print
isSelected()again after the labelAfter click:.
Common mistakes
Clicking without checking first
If a box is already ticked,
click()unticks it, and no error tells you. Make "ensure ticked" helpers checkisSelected()before clicking.Clicking a hidden box
The boxes in the hidden row have
visibility: hidden. Clicking one throwsElementNotInteractableException: element not interactable.Reading the state with getText()
A checkbox input has no text, so
getText()returns an empty string. The text you see beside it belongs to thelabel.
In a real test suite
Checkboxes show up in consent forms, filter panels and "remember me" options. Tests usually tick one, submit, and then check that the setting stuck, for example that a filter was applied or a preference was saved after a reload.
Lesson 21 read styles. This lesson ticks a box; lesson 23 does the opposite with the box that loads ticked, and lesson 34 looks at isSelected() on its own.
Related lessons
- Handle Pre-Selected CheckboxesUntick a box that loads already ticked.
- Handle Radio ButtonsRadio buttons, where clicking one option clears another.
- isSelected() - Check Selection State
isSelected()across ticked and unticked boxes.
To try one of these calls on any page without a task, open the online Selenium compiler for Java.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://testkru.com/Elements/Checkboxes");
// TODO: Find checkbox firstSelect1
// TODO: Print isSelected() before clicking
// TODO: Click the checkbox
// TODO: Print isSelected() after clicking
driver.quit();
}
}