Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Playwright says the execution context was destroyed because of a navigation

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

  • Applies to Playwright for Node.js (current docs), Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1The failing line comes straight after click(), press('Enter') or a form submitA click starts a navigation and the next call runs too earlyFix
    2. 2Your code uses page.waitForNavigation() or Promise.all with itThe wait itself is the racy deprecated waitForNavigationFix
    3. 3The failing call is isVisible(), a boolean read or a manual evaluate used as an assertionOne-shot checks run before the page settlesFix
    4. 4The 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

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

2Fix

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

Confirmed by playwright.dev

Did this fix it?

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 none

Confirmed by playwright.dev

Did this fix it?

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

Confirmed by playwright.dev

Did this fix it?

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

2Fix

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

Did this fix it?

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

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 fix

Next step

Want this handled for good?

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