playwright target page, context or browser has been closed
Fixes Playwright 'target page, context or browser has been closed' by fixing close ordering and CI crashes. Use when actions run against a closed target. Not for selector timeouts.
TL;DR
Playwright throws this when your code touches a page, context, or browser after it was closed. The usual shape is an awaited action racing a close in a fixture, or the whole browser crashing (often out of memory or missing system libraries in CI). Fix the ordering first; if the browser itself died, the launch logs tell you why.
Error
Error: Target page, context or browser has been closedSteps
- Find the close: search the test for
browser.close(),context.close(),page.close(), and any fixture teardown that runs before the failing line. Expected: you spot the action that runs after a close. - Await in order: make sure closes are awaited and happen after the last action, e.g. close in afterEach or afterAll, not mid-test. Expected: no action runs on a closed target.
- If nothing in your code closes it, the browser crashed: re-run with
DEBUG=pw:browserto see the crash output. Expected: the logs show either a clean close or a crash reason. - In CI, install system dependencies:
npx playwright install --with-deps chromium. Expected: missing shared libraries stop killing the browser at launch. - Reduce crash odds in containers: launch with
args: ['--disable-dev-shm-usage']or give the container more memory. Expected: renderer out-of-memory crashes disappear.
When to use
- The error appears right after a navigation or click.
- It happens only in CI or Docker, never locally.
- It started after adding a fixture that closes pages or contexts.
When not to use
Timeout 30000ms exceededwaiting for a selector (a selector or timing issue).- The error appears at the very start before anything runs (launch failed; check the install).
- The browser window visibly crashes with an OS dialog (read the dialog, not this skill).
Tool compatibility
- Playwright 1.30 and newer, Chromium, Firefox, and WebKit.
- Node.js, Python, Java, and .NET bindings.
Variant phrasings
Target closed
The short form of the same error; same diagnosis.
happens on page.screenshot after the test ends
Teardown ordering: the screenshot runs after the fixture closed the page. Move it before the close.
intermittent in parallel runs
Resource exhaustion under parallelism. Fewer workers or more memory.
Why it happens
Every Playwright action goes through the browser process; once the target closes (your code closed it, the test ended, or the process crashed), the pending protocol call has nowhere to go. In CI the crash is usually missing OS libraries or shared-memory exhaustion killing Chromium.
Edge cases
DEBUG=pw:browseroutput is the fastest crash diagnosis; read it before changing code.- Closing the context closes its pages, so the safe order is pages first, then context, then browser.
- Slowing the test down does not help a crash; it only helps a timeout.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst0g2HaMVbIBUmw69GoXReQ