Playwright Example >> Checkbox

2022-03-23 Playwright

Table of Contents

In this article, we will use Playwright (Python version) to check checkbox in the web form. And uncheck it.

Playwright Checkbox

By using Playwright, input[type=checkbox], [role=checkbox] or label associated with checkbox can be checked or unchecked.

Check checkbox

We will use https://demoqa.com/automation-practice-form to test our code.
There are three Checkbox elements in the form as shown below.
Playwright Checkbox

  1. locator() + click()
    You can use locator() to find the checkbox element firstly and use click() method to check it.
    Below we will use the label text of checkbox to locate the checkbox.
page.locator("text=Sports").click()
  1. check()
    You can also use check() method of page object directly to check the checkbox.
page.check("text=Reading")
  1. locator() + check()
    You can use locator() to find the checkbox element firstly and use check() method to check it.
  page.locator("text=Music").check()

You will find all the three checkbox are checked in three different ways.
Playwright Checkbox

Uncheck checkbox

Similarly, we can uncheck the checkboxes in three ways.

  1. locator() + click()
    You can also use the combination of locator() and click() to uncheck the checked checkbox.
page.locator("text=Sports").click()
  1. uncheck()
    You can also use uncheck() method of page object directly to uncheck the checked checkbox.
page.uncheck("text=Reading")
  1. locator() + uncheck()
    You can use the combination of locator() and uncheck() method to uncheck the checked checkbox.
page.locator("text=Music").uncheck()

Conclusion

Check checkbox

  • locator() + click()
  • check()
  • locator() + check()

Uncheck checkbox

  • locator() + click()
  • uncheck()
  • locator() + uncheck()

If you want to learn more about Playwright usage, you can read the article below.

Playwright » Basic Usage
https://thats-it-code.com/playwright/playwright__basic-usage/

You can read the articles below to create a programming environment in your PC.

Let’s Create a Programming Environment
https://thats-it-code.com/programming/lets-create-a-programming-environment/

How to Create Python Virtual Environment
https://thats-it-code.com/programming/how-to-create-python-virtual-environment/

If you want to learn the usage of Radio button, you can read the article below.

Playwright Example » RadioButton
https://thats-it-code.com/playwright/playwright-example__radiobutton/

Subscribe and be the FIRST reader of our latest articles

* indicates required

Contact us