Playwright says the execution context was destroyed because of a navigation
page.evaluate: Execution context was destroyed, most likely because of a navigation.Short answer
The page navigated while your script was running code inside it, so the JavaScript context it was talking to disappeared. Wait for the navigation to finish with page.waitForURL before touching the new page, use locators and web-first assertions instead of one-shot checks, and catch the race inside evaluate.
Fix it fast
1Find out which cause you have
Find the line named in the stack trace and look at the step right before it. If that step is a click, a form submit or a goto, the page navigates while the next call runs.
grep -n "evaluate" tests/*.spec.ts2Match what you see, then jump to its fix
- 1
The failing line comes straight after click(), press('Enter') or a form submitA click starts a navigation and the next call runs too earlyFix - 2
Your code uses page.waitForNavigation() or Promise.all with itThe wait itself is the racy deprecated waitForNavigationFix - 3
The failing call is isVisible(), a boolean read or a manual evaluate used as an assertionOne-shot checks run before the page settlesFix - 4
The page changes by itself, for example a timed redirect, and no action of yours triggers itA redirect you cannot predict lands in the middle of evaluate (scraping)Fix
- 1
What does the error look like?
page.evaluate: Execution context was destroyed, most likely because of a navigation.
Cause 1 of 4
A click starts a navigation and the next call runs too early
1Check
The docs recommend waiting for a URL explicitly when a click can cause several navigations. Without that wait, evaluate runs in the old page while the new one loads.
grep -n "evaluate\|\\$eval" tests/*.spec.ts2Fix
Wait for the destination URL before you run anything in the page.
await page.getByText('Sign in').click();
await page.waitForURL('**/dashboard');
const title = await page.evaluate(() => document.title);3Verify
Run the test several times; the error no longer shows up between the click and the next step.
npx playwright test --repeat-each=5Confirmed by playwright.dev
Cause 2 of 4
The wait itself is the racy deprecated waitForNavigation
1Check
Playwright marks waitForNavigation as deprecated because it is inherently racy. It can also settle on the last of several redirects while your code assumed the first.
grep -rn "waitForNavigation" tests/2Fix
Replace it with waitForURL, which waits for the page to reach a URL instead of listening for an event.
await page.getByRole('button', { name: 'Continue' }).click();
await page.waitForURL(/\/checkout/);3Verify
No waitForNavigation is left and the suite passes repeatedly.
grep -rn "waitForNavigation" tests/ || echo noneConfirmed by playwright.dev
Cause 3 of 4
One-shot checks run before the page settles
1Check
Locators retry and auto-wait. A call like isVisible() answers once and returns immediately, so it runs at whatever moment the page happens to be in.
grep -rn "isVisible()\|textContent()" tests/2Fix
Use a locator with a web-first assertion; Playwright then waits until the condition is met.
await expect(page.getByText('Welcome')).toBeVisible();3Verify
The assertion waits through the page change and passes.
npx playwright test tests/login.spec.tsConfirmed by playwright.dev
Cause 4 of 4
A redirect you cannot predict lands in the middle of evaluate (scraping)
1Check
When no action of yours causes the navigation, there is nothing to wait for. A Playwright maintainer's guidance on this error is to keep the page-side code in its own try/catch.
grep -n "evaluate" scrape.js2Fix
Catch errors inside the evaluated function and return them as values. An error that is thrown outside of evaluate is then a navigation error, and you can retry from the new page.
const result = await page.evaluate(() => {
try {
return { ok: true, value: document.title };
} catch (e) {
return { ok: false, error: String(e) };
}
}).catch(() => ({ ok: false, error: 'navigation' }));3Verify
A navigation in the middle gives you a result object you can retry on, not a crash.
console.log(result);Confirmed by github.com
How do I stop it coming back?
- After every action that can change the page, wait for the URL or an element on the new page.
- Prefer locators and expect assertions to manual evaluate and isVisible checks.
- Remove waitForNavigation from older test code.
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
Web apps
Playwright opens a small window instead of a full-size one
page.viewportSize() is 1280x720 on a larger monitor
Playwright gives every page a fixed 1280x720 viewport, so the page never fills the window.
3 causes, each with a fixWeb apps
Playwright cannot open a headed browser on a Linux server or in Docker
npx playwright test --headed shows no window on a Linux server
A headed browser needs a display, and a Linux server or container has none.
3 causes, each with a fixWeb apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fix