Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Playwright says the target page, context or browser has been closed

ErrorError: locator.click: Target page, context or browser has been closed

Short 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.

  • Applies to Playwright 1.x for Node.js, Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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'));
  2. 2Match what you see, then jump to its fix

    1. 1The error shows in a later step, or after the test already endedA Playwright call is missing its awaitFix
    2. 2The 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. 3The 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. 4browserType.launch or launchPersistentContext fails right at the startTwo browser instances use the same user data directoryFix

What does the error look like?

  • Error: locator.click: Target page, context or browser has been closed
  • browserType.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 test

Confirmed by playwright.dev

Did this fix it?

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.mjs

Confirmed by playwright.dev

Did this fix it?

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=line

2Fix

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 test

Confirmed by playwright.dev

Did this fix it?

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 chrom

2Fix

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.mjs

Confirmed by playwright.dev

Did this fix it?

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

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.