Version: Next


Playwright allows listening to various types of events happening in the web page, such as network requests, creation of child pages, dedicated workers etc. There are several ways to subscribe to such events:

Waiting for event#

Most of the time, scripts will need to wait for a particular event to happen. Below are some of the typical event awaiting patterns.

Wait for a request with the specified url:

// The callback lambda defines scope of the code that is expected to
// trigger request.
Request request = page.waitForRequest("**/*logo*.png", () -> {

Wait for popup window:

// The callback lambda defines scope of the code that is expected to
// create popup window.
Page popup = page.waitForPopup(() -> {

Adding/removing event listener#

Sometimes, events happen in random time and instead of waiting for them, they need to be handled. Playwright supports traditional language mechanisms for subscribing and unsubscribing from the events:

page.onRequest(request -> System.out.println("Request sent: " + request.url()));
Consumer<Request> listener = request -> System.out.println("Request finished: " + request.url());
// Remove previously added listener, each on* method has corresponding off*

Adding one-off listeners#

If certain event needs to be handled once, there is a convenience API for that:

page.onceDialog(dialog -> dialog.accept("2021"));
page.evaluate("prompt('Enter a number:')");

API reference#