Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

This error usually means Puppeteer tried to evaluate JavaScript in a page context that disappeared during navigation or reload. Start a navigation wait before the action that may navigate, wait for the actual rendered state when there is no full navigation, and reacquire element handles after the document changes.

What “Execution Context Was Destroyed” means

A Puppeteer execution context is the JavaScript environment associated with a page document or frame. When Chrome reports that a context was destroyed or cleared, Puppeteer disposes of it. An evaluation that was using that context can then fail. In Puppeteer Core 24.26.1, the implementation also translates certain protocol errors, including “Cannot find context with specified id,” into the message “Execution context was destroyed, most likely because of a navigation.” Puppeteer Core source

Navigation and reload are common triggers, but the message alone does not identify which operation caused the context to disappear. Look at the action, the frame being used, and what the page was doing when the failure occurred. Historical issue reports describe reloads and redirects, but only for the versions and sites in those reports. Puppeteer issue #10057

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the right wait for the page change

The key distinction is whether the next action depends on a navigation or on rendered content within the current document. Puppeteer’s API describes waitForNavigation() as waiting “for the page to navigate to a new URL or to reload.” Page.waitForNavigation() API

Situation Use Why
A click, submit, reload, or other action may navigate or replace the document Start page.waitForNavigation() before the action, then await both Prevents the wait from being installed after navigation has already begun
The current document remains, but you need a particular element or application result Wait for the relevant selector or application-state predicate Tests the actual readiness condition instead of assuming a full navigation

Fix a click or form submission that navigates

Create the navigation wait before triggering the action. Puppeteer documents coordinating the two promises with Promise.all; starting a separate wait after the click can miss a fast navigation. Page.waitForNavigation() API Page.click() API

const [response] = await Promise.all([
  page.waitForNavigation(),
  page.click('a.my-link'),
]);

For a form submission or another navigation-triggering action, use the same ordering and replace page.click() with that action. Set waitUntil only to the lifecycle condition the next step needs; a heavier wait is not automatically better. waitForNavigation() resolves with the main resource response, but returns null for anchor changes and History API URL changes. A null response alone therefore does not establish that the wait failed. Check the expected URL or rendered state too. Page.waitForNavigation() API

Wait for a selector or application state instead

If the page does not replace its document, waiting for a full navigation may be the wrong signal. Wait for the element needed by the next operation:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
await page.waitForSelector('.results-ready');
const result = await page.$eval('.results-ready', element => element.textContent);

Puppeteer documents that selector waits work across navigations. For a single-page application, choose a selector or predicate that represents the rendered result your script needs, rather than waiting for a document navigation that may never occur. Avoid arbitrary sleeps when a specific page condition can be observed. Page.waitForSelector() API ZenRows: Puppeteer wait for page to load

Reacquire element handles after the document changes

An element handle refers to an element in a particular document context. If navigation replaces that document, a handle acquired before the change may no longer be usable. Query the current page again after the navigation wait or readiness check instead of carrying the old handle into the next step. ZenRows: Puppeteer execution context was destroyed

Check which frame is navigating

When the failing operation targets an iframe, establish which frame owns the element and whether that frame navigated or detached. Page-level operations commonly target the main frame; Puppeteer also provides frame-specific evaluation and waiting methods. A main-frame navigation wait does not by itself prove a child frame is ready. Use the relevant frame’s selector or state as the readiness signal. Puppeteer Frame API

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Debug the failure in order

  1. Identify the possible context replacement. Check for goto, reload, goBack, a link click, form submission, or script-triggered navigation near the failing operation.
  2. Coordinate navigation when it is expected. Start waitForNavigation() first and await it with the triggering action.
  3. Wait for rendered state when navigation is not expected. Use the required selector or an application-state predicate.
  4. Refresh references after replacement. Reacquire elements from the current document.
  5. Verify the frame. Confirm whether the operation concerns the main frame or a child frame, then wait on that frame’s state.
  6. Inspect the first failure. Log the URL and frame around the action. Add a bounded retry only if repeating the action is safe; repeating a form submission or other side effect may not be.

Or skip the browser setup:

If the task is simply to capture a website image or PDF rather than automate an interactive browser workflow, ScreenshotNeo offers a screenshot API and MCP server. Its clean-capture options can accept consent banners and remove supported overlays before capture; bot checks, blank pages, timeouts, failed loads, and cache hits are not billed. ScreenshotNeo

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use an API key in place of YOUR_API_KEY; this example captures Stripe as a WebP image. See the ScreenshotNeo API documentation.

Best Value
The SQL Programming Language: .
  • Used Book in Good Condition
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.