Skip to content
CodeKruPro

Handle Checkboxes - Click and Verify

22/70

Video walkthrough

Watch on YouTube

Handle Checkboxes

Use click() to toggle a checkbox and isSelected() to verify its state.

On the Checkboxes page, the single-select checkbox id="firstSelect1" is initially unchecked.

Task:

  1. Navigate to https://testkru.com/Elements/Checkboxes
  2. Find checkbox firstSelect1
  3. Print its state (should be false)
  4. Click it to select it
  5. Print its state (should be true)

Expected output:

Before click: false
After click: true
Read full tutorial on Codekru

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

  1. Find the checkbox by its id.
  2. Print isSelected() after the label Before click:.
  3. Click the checkbox once.
  4. Print isSelected() again after the label After 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 check isSelected() before clicking.

  • Clicking a hidden box

    The boxes in the hidden row have visibility: hidden. Clicking one throws ElementNotInteractableException: 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 the label.

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.

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.

Main.java
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();
    }
}
Read the full tutorial