Puppeteer: JSHandles can be evaluated only in the context they were created!
A JSHandle is evaluated in a different execution context from its owner.
A JSHandle is evaluated in a different execution context from its owner.
Error message
JSHandles can be evaluated only in the context they were created!
How this error happens
A handle belongs to the page and execution realm where it was created. A different page cannot use that reference as a local object.
The snippets use an open page and its running browser. The fix continues from the handles created before the failing call.
const otherPage = await browser.newPage();
const handle = await page.evaluateHandle(() => {
return {
count: 3,
};
});
await otherPage.evaluate(
(value) => value.count,
handle
);
How to fix
Evaluate the handle in its owning page. If the other page needs the data, transfer a plain value instead of a handle.
const count = await page.evaluate(
(value) => value.count,
handle
);
await otherPage.evaluate(
(value) => {
window.count = value;
},
count
);
Things to keep in mind
Close otherPage and dispose handle after use. CDP reports an execution-context mismatch; BiDi reports a frame mismatch. Separate frames in one page also have separate realms.