Find the live browser
For an agent run, watch its ordered run events forbrowser.ready or
browser.reattached. The event’s data.browser_session_id identifies the browser.
You can also list the session’s browsers with
GET /api/v4/browsers?agentSessionId=SESSION_ID. A session can use more than one
browser over time, so do not assume the first browser lasts for every follow-up.
Use an API key with browser read access in the same project to retrieve the
active browser’s cdpUrl. BROWSER_SESSION_ID is a browser ID, not a run ID or
agent session ID.
cdpUrl from the create response
instead. A stopped browser has no live CDP URL. Check the HTTP status and browser
state if discovery fails; do not create a replacement browser and mistake it for
the agent’s browser.
Record metadata from existing pages
Install Playwright withpip install playwright. No local browser download is
needed because this example connects to an existing browser.
Save this as capture_network.py, then run python capture_network.py. It records
for 30 seconds and creates network.jsonl with owner-only permissions. It refuses
to overwrite an existing file. Start it before the activity you want to observe.
(page, requestId). A response row gives the HTTP
status; a finished row gives transferred bytes. Keep the event sequence: redirects
can produce more than one request event with the same request ID. Empty output
means no matching events were observed, not that the run made no requests.
The example does not navigate, intercept requests, change the cache, or stop the
browser. It drops URL paths, queries, credentials, headers, cookies, and request
and response bodies. HTTP origins retain non-default ports; malformed and non-HTTP
URLs have a null origin. Origins can still be sensitive; review the file before
sharing it. Keep a bounded capture duration and a retention policy.
Coverage and lifecycle
- Capture starts only after
Network.enablefor each attached page. Earlier traffic is not reconstructed, and attaching afterbrowser.readycan miss startup requests. - This small example attaches only to pages present at startup. Popups, new tabs, workers, service workers, and some cross-process frames need separate target handling. It is not complete browser-wide capture.
- Reattach if V4 provisions a new browser. A logger attached to the old browser cannot observe the replacement.
- JSONL events are not HAR. A HAR exporter needs its own request, redirect,
timing, and body handling.
Network.getResponseBodyis a separate, optional CDP call; bodies may be unavailable and can contain secrets or customer data. - Detaching an observer does not stop a browser or end its billing. Leave an agent-owned browser under the agent’s lifecycle. For a standalone browser you created, stop it through the browser API when finished.