Find an element in Playwright java

Find a single element in Playwright

The querySelector(locator) method searches and locates the first element on the current page, which matches the locator criteria given as a parameter. querySelector(locator) method is usually used in commands to simulate user actions like click, submit, type, etc.

page.querySelector("#id");
Find multiple elements in
Playwright

Sometimes we might want to perform an action on a list of web elements that match the given selector, In such a case, we use querySelectorAll(Locator).

page.querySelectorAll("#id");

The complete program

import com.microsoft.playwright.*;

public class Example {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium().launch(
    		  new BrowserType.LaunchOptions().setHeadless(false)
    		  );
      Page page = browser.newPage();
      page.navigate("http://playwright.dev");
      // find element
      page.querySelector("#id");
      //find elements
      page.querySelectorAll("#id");
      page.close();
      browser.close();
    }
  }
}
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions