Puppeteer: Execution context is not available in detached frame or worker '{value}' (are you tryin...
The frame or worker owning evaluation is detached, so its execution context is unavailable.
The frame or worker owning evaluation is detached, so its execution context is unavailable.
Error message
Execution context is not available in detached frame or worker '{value}' (are you tryin...
{value} is replaced by the value shown in your error message.
How this error happens
A frame’s JavaScript context disappears when its iframe is removed. Keeping a reference does not keep that context alive.
The snippets use an open Puppeteer page named page.
await page.setContent(
'<iframe srcdoc="<p>Hello</p>"></iframe>'
);
const iframe = await page.$("iframe");
const frame = await iframe.contentFrame();
await page.$eval("iframe", (element) => {
element.remove();
});
await frame.evaluate(() => {
return document.title;
});
How to fix
Obtain a live frame from the current DOM after replacement. Wait for its content before evaluating.
await page.setContent(
'<iframe srcdoc="<p id=message>Hello again</p>"></iframe>'
);
const iframe = await page.waitForSelector(
"iframe"
);
const frame = await iframe.contentFrame();
await frame.waitForSelector(
"#message"
);
const text = await frame.$eval(
"#message",
(element) => element.textContent
);
console.log(text);
Things to keep in mind
A terminated worker has the same ownership problem. Get the current worker and recreate its handles. A detached-frame guard may report a related message before context lookup reaches this exact check.