Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Playwright: check or uncheck the checkbox using setChecked method

Using Locator.setChecked() method, we can check or uncheck the checkbox.

 

Signature

void setChecked(boolean checked)
void setChecked(boolean checked, SetCheckedOptions options)

Example

page.getByLabel("java").setChecked(true);
page.getByLabel("go").setChecked(false);

Find the below working application.

 

checkOrUncheck.html


html>

body>

  h1>User favorite languages formh1>

  p>Select your favorite languages and click on submit buttonp>

  form onsubmit="return false;">
      input type="checkbox" id="java" name="my_fav_language" value="Java">
      label for="java">Javalabel>br>

      input type="checkbox" id="go" name="my_fav_language" value="Go">
      label for="go">Golabel>br>

      input type="checkbox" id="python" name="my_fav_language" value="Python">
      label for="python">Pythonlabel>br>br>

    input type="submit" value="submit" onclick="display()">
  form>

  p id="result">p>

  script>
    function display() {
      let result = document.getElementById("result");
      let favoriteLanguages = "
"
; var markedCheckbox = document.getElementsByName('my_fav_language'); for (var checkbox of markedCheckbox) { if (checkbox.checked) favoriteLanguages = favoriteLanguages + "
"
+ checkbox.value; } result.innerHTML = `Your favourite language are : ${favoriteLanguages}` }
script> body> html>

FileUtil.java

package com.sample.app.locators;

import java.io.File;
import java.io.IOException;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.sample.app.util.FileUtil;

public class CheckOrUncheck {

  public static void main(String[] args) throws IOException {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium()
          .launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));
      final String content = FileUtil.resourceAsString("actions" + File.separator + "checkbox.html");

      Page page = browser.newPage();
      page.setContent(content);

      System.out.println("Selecting Java, go and python langauges");
      page.getByLabel("java").setChecked(true);
      page.getByLabel("go").setChecked(true);
      page.getByLabel("python").setChecked(true);

      System.out.println("Is Java Language Checked : " + page.getByLabel("java").isChecked());
      System.out.println("Is Go language checked : " + page.getByLabel("go").isChecked());
      System.out.println("Is Python Language Checked : " + page.getByLabel("python").isChecked());

      System.out.println("De-selecting Java, go and python langauges");
      page.getByLabel("java").setChecked(false);
      page.getByLabel("go").setChecked(false);
      page.getByLabel("python").setChecked(false);

      System.out.println("\nIs Java Language Checked : " + page.getByLabel("java").isChecked());
      System.out.println("Is Go language checked : " + page.getByLabel("go").isChecked());
      System.out.println("Is Python language checked : " + page.getByLabel("python").isChecked());

    }
  }

}

Output

Selecting Java, go and python langauges
Is Java language checked : true
Is Go language checked : true
Is Python language checked : true
De-selecting Java, go and python langauges

Is Java language checked : false
Is Go language checked : false
Is Python language checked : false


Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Playwright: check or uncheck the checkbox using setChecked method

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×