Playwright says the target page, context or browser has been closed
Error: locator.click: Target page, context or browser has been closedShort answer
The error means Playwright received a command for a page, context or browser that was already closed. The usual cause is code that keeps running after the test or script closed it: a missing await, a close call placed too early, or a browser that crashed. Find the first call after the close, not the line the error points at.
Fix it fast
1Find out which cause you have
Log when the page, context and browser close, so you can see whether something was closed before the failing call.
page.on('close', () => console.log('page closed')); context.on('close', () => console.log('context closed')); browser.on('disconnected', () => console.log('browser disconnected'));2Match what you see, then jump to its fix
- 1
The error shows in a later step, or after the test already endedA Playwright call is missing its awaitFix - 2
The error follows a close() call in your script, a finally block or an afterAll hookThe context or browser is closed while work is still pendingFix - 3
The error is random, shows in CI, and the browser logs print an error before itThe browser crashed or was killed while the test ranFix - 4
browserType.launch or launchPersistentContext fails right at the startTwo browser instances use the same user data directoryFix
- 1
What does the error look like?
Error: locator.click: Target page, context or browser has been closedbrowserType.launch: Target page, context or browser has been closed
Cause 1 of 4
A Playwright call is missing its await
1Check
A call without await runs in the background while the script moves on. The test then finishes and closes the page while that call is still waiting. Linting finds these.
npx eslint --rule '@typescript-eslint/no-floating-promises: error' tests/2Fix
Await every Playwright call, and turn on the no-floating-promises lint rule so a missing await fails in the editor.
// wrong
page.getByRole('button', { name: 'Save' }).click();
// right
await page.getByRole('button', { name: 'Save' }).click();3Verify
Run the test again. The error should be gone, or point at the real failing step.
npx playwright testConfirmed by playwright.dev
Cause 2 of 4
The context or browser is closed while work is still pending
1Check
browserContext.close() closes every page in that context, and browser.close() closes the browser with all its pages. Work still running on those pages is interrupted.
grep -rn "\.close()" tests/ src/2Fix
Wait for all page work to finish before you close, and close contexts before the browser. Use Promise.all for parallel pages.
const results = await Promise.all(urls.map(async (url) => {
const page = await context.newPage();
await page.goto(url);
return page.title();
}));
await context.close();
await browser.close();3Verify
Run the script. Every page finishes before the close call.
node scrape.mjsConfirmed by playwright.dev
Cause 3 of 4
The browser crashed or was killed while the test ran
1Check
The disconnected and close events also fire when the browser application is closed or crashed. Print the browser logs next to the error, because the real reason is there.
npx playwright test --reporter=line2Fix
Fix what the browser log shows, such as a missing system library or too little memory on the CI machine.
browser.on('disconnected', () => {
console.error('browser disconnected: check the browser log above this line');
});3Verify
Run the same suite again and confirm the browser log now shows the reason.
npx playwright testConfirmed by playwright.dev
Cause 4 of 4
Two browser instances use the same user data directory
1Check
Browsers do not allow launching multiple instances with the same user data directory, and pointing it at your normal browser profile can make the browser exit.
ps aux | grep -i chrom2Fix
Give each automated run its own empty user data directory, and close other browsers using the same profile.
import { chromium } from 'playwright';
import os from 'node:os';
import path from 'node:path';
import fs from 'node:fs';
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pw-profile-'));
const context = await chromium.launchPersistentContext(dir);3Verify
Launch twice at the same time. Both start.
node launch.mjs & node launch.mjsConfirmed by playwright.dev
How do I stop it coming back?
- Enable the no-floating-promises lint rule in every project that uses Playwright.
- Close pages, then contexts, then the browser, in that order, and only after all work has finished.
- Keep each run on its own user data directory.
- Keep browser logs in CI artifacts so a crash leaves a trace.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Playwright browser download fails behind a company proxy with a certificate error
Error: self signed certificate in certificate chain
Playwright downloads browsers over HTTPS, and a proxy that re-signs traffic with its own root certificate makes Node.js reject the connection.
3 causes, each with a fixBack end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fix