Puppeteer: Execution context was destroyed, most likely because of a navigation.
Navigation destroyed the execution context used by evaluation.
Navigation destroyed the execution context used by evaluation.
Error message
Execution context was destroyed, most likely because of a navigation.
How this error happens
Navigation replaces the document and its execution context. An evaluation that is still waiting in the old document cannot finish in the new one.
The snippets use an open Puppeteer page named page.
const reading = page.evaluate(() => {
return new Promise(() => { });
});
reading.catch(() => { });
await page.goto(
"about:blank?next"
);
await reading;
How to fix
Finish the navigation before starting evaluation in the new document.
await page.goto(
"about:blank?next"
);
const url = await page.evaluate(() => {
return location.href;
});
console.log(url);
Things to keep in mind
The empty catch only prevents an unhandled rejection while the demonstration navigates; await reading still observes the error. For click-triggered navigation, register waitForNavigation before the click.