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
- An earlier async Playwright call (often page.goto) wasn't awaited, so the test moved on and closed the browser before the click fired
- A previous step called page.close() / context.close() / browser.close() too early, while another action was still in flight
- A hook (beforeEach/afterEach) tore down the context while an assertion or action from the test body was still running
How to fix it
- Await every Playwright API call: a dangling promise is the #1 cause of this error
- Don't manually close the page/context/browser inside a test; let the test runner's fixtures manage lifecycle
- 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();