Skip to main content
Version: 1.22


Playwright provides APIs to monitor and modify network traffic, both HTTP and HTTPS. Any requests that page does, including XHRs and fetch requests, can be tracked, modified and handled.

HTTP Authentication

BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setHttpCredentials("bill", "pa55w0rd"));
Page page = context.newPage();

API reference

HTTP Proxy

You can configure pages to load over the HTTP(S) proxy or SOCKSv5. Proxy can be either set globally for the entire browser, or for each browser context individually.

You can optionally specify username and password for HTTP(S) proxy, you can also specify hosts to bypass proxy for.

Here is an example of a global proxy:

Browser browser = chromium.launch(new BrowserType.LaunchOptions()
.setProxy(new Proxy("")

When specifying proxy for each context individually, Chromium on Windows needs a hint that proxy will be set. This is done via passing a non-empty proxy server to the browser itself. Here is an example of a context-specific proxy:

Browser browser = chromium.launch(new BrowserType.LaunchOptions()
// Browser proxy option is required for Chromium on Windows.
.setProxy(new Proxy("per-context"));
BrowserContext context = chromium.launch(new Browser.NewContextOptions()
.setProxy(new Proxy(""));

Network events

You can monitor all the requests and responses:


public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium();
Browser browser = chromium.launch();
Page page = browser.newPage();
page.onRequest(request -> System.out.println(">> " + request.method() + " " + request.url()));
page.onResponse(response -> System.out.println("<<" + response.status() + " " + response.url()));

Or wait for a network response after the button click:

// Use a glob URL pattern
Response response = page.waitForResponse("**/api/fetch_data", () -> {"button#update");


// Use a RegExp
Response response = page.waitForResponse(Pattern.compile("\\.jpeg$"), () -> {"button#update");

// Use a predicate taking a Response object
Response response = page.waitForResponse(r -> r.url().contains(token), () -> {"button#update");

API reference

Handle requests

page.route("**/api/fetch_data", route -> route.fulfill(new Route.FulfillOptions()

You can mock API endpoints via handling the network requests in your Playwright script.


browserContext.route("**/api/login", route -> route.fulfill(new Route.FulfillOptions()

API reference

Modify requests

// Delete header
page.route("**/*", route -> {
Map<String, String> headers = new HashMap<>(route.request().headers());
route.resume(new Route.ResumeOptions().setHeaders(headers));

// Continue requests as POST.
page.route("**/*", route -> route.resume(new Route.ResumeOptions().setMethod("POST")));

You can continue requests with modifications. Example above removes an HTTP header from the outgoing requests.

Abort requests

page.route("**/*.{png,jpg,jpeg}", route -> route.abort());

// Abort based on the request type
page.route("**/*", route -> {
if ("image".equals(route.request().resourceType()))

API reference


Playwright supports WebSockets inspection out of the box. Every time WebSocket is created, Page.onWebSocket(handler) event is fired. This event contains the WebSocket instance for further web socket frames inspection:

page.onWebSocket(ws -> {
log("WebSocket opened: " + ws.url());
ws.onFrameSent(frameData -> log(frameData.text()));
ws.onFrameReceived(frameData -> log(frameData.text()));
ws.onClose(ws1 -> log("WebSocket closed"));

API reference