Playwright error

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

Playwright throws this when the page, context, or browser is torn down before an action resolves. It's almost always a missing await.

Why this happens

How to fix it

  1. Await every Playwright API call: a dangling promise is the #1 cause of this error
  2. Don't manually close the page/context/browser inside a test; let the test runner's fixtures manage lifecycle
  3. If you race a timeout against an action (Promise.race), make sure the losing branch can't close the page out from under the winner

Example


              // Bad: goto isn't awaited, test can race ahead and close the browser
page.goto('/checkout');
await page.locator('#buy').click();

// Good
await page.goto('/checkout');
await page.locator('#buy').click();