Playwright Java get driver paths of browsers.

Sameera De Silva
2 min readDec 1, 2023

--

To achieve this, I used below code.

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import org.testng.annotations.Test;

import java.io.IOException;

public class DriverPathsOfBrowsers {

@Test
public void firstTest() throws IOException {

// We can take screenshots in headless mode too.
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
// This line creates a new page object within the browser instance. The page object represents a single web page that can be navigated, interacted with, and tested.(It's kind of tab but exactly a tab.)
Page page = browser.newPage();
// This code instructs the page object to navigate to the specified URL
page.navigate("https://the-internet.herokuapp.com/");
String driverPathOfChromium = playwright.chromium().executablePath();
System.out.println("Driver path of chromium: " + driverPathOfChromium);

/*
Playwright uses its own Firefox build.
That build is based on the Firefox Nightly build. That's why you are getting that browser. You won't be able to use the stock browser version.
So it will be Nightly Browser
*/
String driverPathOfFirefox = playwright.firefox().executablePath();
System.out.println("Driver path of Firefox: " + driverPathOfFirefox);
String driverPathOfWebKit= playwright.webkit().executablePath();
System.out.println("Driver path of WebKit: " + driverPathOfWebKit);

playwright.close();
}
}

This will give below output.

Driver path of chromium: C:\Users\myUserName\AppData\Local\ms-playwright\chromium-1060\chrome-win\chrome.exe
Driver path of Firefox: C:\Users\myUserName\AppData\Local\ms-playwright\firefox-1403\firefox\firefox.exe
Driver path of WebKit: C:\Users\myUserName\AppData\Local\ms-playwright\webkit-1837\Playwright.exe

So using that we can launch playwright with custom driver path.

I created a folder called ArchivedPlaywrightDrivers and moved everything in “C:\Users\myUserName\AppData\Local\ms-playwright\chromium-1060\chrome-win” to it .

import org.testng.annotations.Test;
import com.microsoft.playwright.*;

import java.nio.file.Path;
import java.nio.file.Paths;

public class LaunchChromiumBrowserWithCustomDriverPath {

@Test
public void customDriverPathChrome() throws InterruptedException {
// Create a Playwright instance
Playwright playwright = Playwright.create();

// Get the path to the Chrome driver
String workingDirectory = System.getProperty("user.dir");
// I had to copy everything in C:\Users\myUserName\AppData\Local\ms-playwright\chromium-1060\chrome-win to ArchivedPlaywrightDrivers
Path driverPathOfChromium = Paths.get(workingDirectory, "ArchivedPlaywrightDrivers");
// Set the path to the Chrome driver executable
String executablePath = driverPathOfChromium.toString() + "\\chrome.exe";

// Launch the Chrome browser using the custom driver path, with none-headless mode.
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
.setExecutablePath(Paths.get(executablePath)).setHeadless(false));
// Create a new page
Page page = browser.newPage();
// Open a web page
page.navigate("https://www.google.com");
Thread.sleep(25000);
// Close the browser
browser.close();
// Close the Playwright instance
playwright.close();
}
}

First time when drivers are downloaded or when there is an update , in console , below message is displayed . This downloadable location can be helpful, if needs to download it manually. Keep in mind that this link is dynamic.

Downloading Chromium 120.0.6099.28 (playwright build v1091) from https://playwright.azureedge.net/builds/chromium/1091/chromium-win64.zip

--

--

No responses yet