Open browser in Playwright Java

By default playwright opens the Chromium Browser, to open the Chromium browser we have to call a method called create(). Once we have the update we can create the chromium Browser by accessing a chromium() method and then launch() method.

While launching the chromium Browser we can set the headless mode, by default play right open the browser in headless mode only. In case if you want to have UI then we can use the setHeadless(false) from the LaunchOptions().

Now we have open the browser, but we have not opened any tab in the browser so using the above-created browser object we can create a new tab using newPage() method.

But respect to playwright and puppeteer, we call the tab as a page.

Once the page is opened we can go to your website using navigate() method.

Open chromium browser with Playwright java
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");
      System.out.println(page.title());
    }
  }
}
How to open Chrome using Playwright java

To open the chrome browser we have to use the setExecutebalePath() method. setExecutebalePath() accepts the Chrome installed location as string. The chrome browser might be installed in C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe on your windows machine.

public class Example {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium().launch(
    		  new BrowserType.LaunchOptions().setHeadless(false)
    		  .setExecutablePath(Paths.get("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"))
    		  );
      Page page = browser.newPage();
      page.navigate("http://playwright.dev");
      System.out.println(page.title());
    }
  }
}
Open Microsoft Edge in Playwright java

To open the MS edge browser we have to use the setExecutebalePath() method. setExecutebalePath() accepts the Edge browser installed location as string.

public class Example {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium().launch(
    		  new BrowserType.LaunchOptions().setHeadless(false)
    		  .setExecutablePath(Paths.get("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"))
    		  );
      Page page = browser.newPage();
      page.navigate("http://playwright.dev");
      System.out.println(page.title());
    }
  }
}
Open Firefox in Playwright java

Playwright installs its own version of Firefox when it downloads the dependencies. We have to call the firefox() method from the playwright object to open the Firefox. You can also use the setExecutablePath() to open firefox that is present in your system.

public class Example {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.firefox().launch(
    		  new BrowserType.LaunchOptions().setHeadless(false)
    		  );
      Page page = browser.newPage();
      page.navigate("http://playwright.dev");
      System.out.println(page.title());
    }
  }
}
Open Safari in Playwright Java

You just have to use the webkit() method from the playwright object to open the Safari browser. Playwright comes with safari bundled.

public class Example {
  public static void main(String[] args) {
	  System.out.println("hello");
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.webkit().launch(
    		  new BrowserType.LaunchOptions().setHeadless(false)
    		  );
      Page page = browser.newPage();
      page.navigate("http://playwright.dev");
      System.out.println(page.title());
    }
  }
}
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