isChecked() method in Playwright Java

isChecked() method in Playwright checks whether a radio button or a checkbox is selected. This post will discuss Playwright’s isChecked() method in detail and will see the working examples using Java.

The isChecked() method is available through the Page, Locator, and ElementHandle implementations. However, it is advisable to use the implementation provided by the Locator.

Locator provides two overloaded isChecked() method –

  • default boolean isChecked()
  • boolean isChecked(IsCheckedOptions options)

Let’s discuss them one by one.

default boolean isChecked()

The isChecked() function will indicate whether a checkbox or radio button has been selected by returning true. It will return false if it is not selected.

We will check whether the below-highlighted checkbox is checked or not. This element is present on our playground website https://testkru.com/Elements/Checkboxes.

Checkbox element

Based on the element’s id, which is firstSelect5, we can effectively locate it using the locator() method provided by Playwright.

Locator locator = page.locator("#firstSelect5");

We can use the isChecked() method on the resulted locator element to check whether the checkbox is checked.

locator.isChecked();

Here is the whole code

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class CodekruTest {

	public static void main(String[] args) {

		Playwright playwright = Playwright.create();
		Browser browser = playwright.chromium().launch();

		// creating a BrowserContext
		BrowserContext browserContext = browser.newContext();

		Page page = browserContext.newPage();

		// Navigating to the URL
		page.navigate("https://testkru.com/Elements/Checkboxes");

		Locator locator = page.locator("#firstSelect5");
		System.out.println("Is checkbox checked: " + locator.isChecked());

		// closing the instances
		browser.close();
		playwright.close();

	}

}

Output –

Is checkbox checked: true

We can see that it returned true because the checkbox was checked. It would have returned false if it wasn’t checked.

What if we use the isChecked() method on other elements ( other than the checkbox or radio button )?

Let’s try to use the isChecked() method on a button. We have such an element on our playground website – https://testkru.com/Elements/Buttons.

Button element

Inspecting the element will reveal its DOM.

DOM of button element

We can use the id attribute value to locate the element.

page.locator("#leftClick");
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class CodekruTest {

	public static void main(String[] args) {

		Playwright playwright = Playwright.create();
		Browser browser = playwright.chromium().launch();

		// creating a BrowserContext
		BrowserContext browserContext = browser.newContext();

		Page page = browserContext.newPage();

		// Navigating to the URL
		page.navigate("https://testkru.com/Elements/Buttons");

		Locator locator = page.locator("#leftClick");
		System.out.println("Is checkbox checked: " + locator.isChecked());

		// closing the instances
		browser.close();
		playwright.close();

	}

}

Output –

Exception in thread "main" com.microsoft.playwright.PlaywrightException: Error {
  message='Error: Not a checkbox or radio button
=========================== logs ===========================
waiting for locator("#leftClick")
============================================================
  name='Error
  stack='Error: Error: Not a checkbox or radio button
=========================== logs ===========================
waiting for locator("#leftClick")

It threw a PlaywrightException stating, “Error: Not a checkbox or radio button”. So, we should only use this method with a checkbox or a radio button.

Internal implementation of the isChecked() method
  default boolean isChecked() {
    return isChecked(null);
  }

isChecked() internally uses the isChecked(IsCheckedOptions options). So, let’s discuss it now.

boolean isChecked(IsCheckedOptions options)

It accepts the options as an argument and checks whether the checkbox or the radio button is selected or not.

What are these options?

An option is an object of the IsCheckedOptions class, which only has one method and a variable.

  class IsCheckedOptions {

    public Double timeout;

    public IsCheckedOptions setTimeout(double timeout) {
      this.timeout = timeout;
      return this;
    }
  }

So, we can now set timeout while using the isChecked() method.

What is the use of timeout in the isChecked() method?

The “timeout” refers to the maximum duration that the “isChecked()” method will wait to verify if a checkbox or radio button is selected. If the verification process exceeds the set time limit, it will trigger a TimeoutError exception.

The default timeout is 30 seconds.

		Locator locator = page.locator("#leftClick");
		
		IsCheckedOptions isCheckedOptions = new IsCheckedOptions();
		isCheckedOptions.setTimeout(10);
		
		System.out.println("Is checkbox checked: " + locator.isChecked(isCheckedOptions));

Please note that the timeout value should be set in milliseconds. As it takes more than 10 milliseconds to verify if a radio button or checkbox is selected. so a TimeoutError exception will be thrown.

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Locator.IsCheckedOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class CodekruTest {

	public static void main(String[] args) {

		Playwright playwright = Playwright.create();
		Browser browser = playwright.chromium().launch();

		// creating a BrowserContext
		BrowserContext browserContext = browser.newContext();

		Page page = browserContext.newPage();

		// Navigating to the URL
		page.navigate("https://testkru.com/Elements/Buttons");

		Locator locator = page.locator("#leftClick");
		
		IsCheckedOptions isCheckedOptions = new IsCheckedOptions();
		isCheckedOptions.setTimeout(10);
		
		System.out.println("Is checkbox checked: " + locator.isChecked(isCheckedOptions));

		// closing the instances
		browser.close();
		playwright.close();

	}

}

Output –

Exception in thread "main" com.microsoft.playwright.TimeoutError: Error {
  message='Timeout 10ms exceeded.
=========================== logs ===========================
waiting for locator("#leftClick")
============================================================
  name='TimeoutError
  stack='TimeoutError: Timeout 10ms exceeded.
=========================== logs ===========================
waiting for locator("#leftClick")

We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

2 thoughts on “isChecked() method in Playwright Java”

  1. Hi! Thanks for the content.

    I wonder if there is any method that I can check whether the button is clicked or not. I have a button and if it’s not clicked I need to click it but if it’s already clicked I can move on.

    but I wasn’t able to find any method that can help me to check whether the element is clicked.
    Could you please help me to find the appropriate one?

    1. Hi Jacky,

      Unfortunately, Playwright doesn’t provide any such inbuilt function as of now that we can use to know if a button was clicked. However, we can achieve a similar functionality by implementing a custom solution.

      We have created a separate post that explains and demonstrates how to implement this custom solution. You can access it through the following link: https://www.codekru.com/playwright/how-to-check-if-a-button-is-clicked-in-playwright-java.

      We hope this helps you.

Leave a Comment

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