How to capture background requests and responses in Playwright
Playwright exposes the network activity of a page through events on the Page object.
Register a callback for the request event to see outgoing calls, and a callback for the response event to see what came back. The two listeners are independent, so you can use either one alone or both together.
How the capture works
The Page object emits a request event for each outgoing call and a response event for each reply. You attach a function to each event with page.on. Playwright then invokes your function with the request or response object every time the event fires, including calls the page makes on its own after the initial navigation.
This is event-driven capture, not a polling loop. Your callbacks run as the traffic happens, so a page that keeps fetching data in the background will keep producing records for as long as the page is open.
What each callback receives
The request callback receives a request object. In the captured example the callback reads request.url and prints it. The response callback receives a response object and reads response.url and response.status. Those three fields are enough to build a basic network log:
- request.url — the address the page asked for.
- response.url — the address that produced the reply.
- response.status — the HTTP status code of the reply.
If you need more than a URL and a status, inspect the request and response objects for the additional properties your Playwright version exposes before you write them into your log.
A worked example
The captured Python example launches Chromium, opens a new page, registers two callbacks, and then navigates. The request callback prints the requested URL. The response callback prints the response URL together with its status. After page.goto runs, every background call the page makes is routed through those two functions.
A practical way to read the output is to pair each request line with the matching response line by URL. If a request appears with no matching response, the call either failed, was cancelled, or is still in flight when your script ends. That gap is often the most useful signal in the log.
Capture versus modify
Listening to the events is the capture step. The same callbacks can also change what the page sends or receives, which is the modification step. Keep the two purposes separate in your own code: a capture-only callback should record and return, while a modifying callback should alter the request or response and then let it continue. Mixing the two makes logs harder to trust.
If your goal is only to observe traffic, do not mutate the objects inside the callback.
Checklist before you rely on the log
- Decide whether you need requests, responses, or both, and register only those listeners.
- Confirm the callbacks are attached before the navigation that triggers the traffic.
- Record at least the URL on each side so request and response lines can be paired.
- Include the status code on responses so failures are visible.
- Keep capture callbacks free of mutation if the log must reflect real traffic.
- Note that background calls continue after the first page load, so keep the page open long enough to collect them.
Frequently asked questions
Do I need both the request and response listeners?
No. Each event is independent. Register only the request listener if you need outgoing URLs, only the response listener if you need status codes, or both when you want to pair calls with their replies.
Can the same callbacks change traffic instead of just logging it?
Yes. The callbacks can modify requests and responses as well as record them. Keep capture and modification in separate callbacks so your log stays accurate.
Why does a request appear without a matching response?
The call may have failed, been cancelled, or still be in flight when your script stops. Pairing lines by URL and checking the status field makes those cases visible.