Puppeteer: Protocol error ({value}): Session closed. Most likely the page has been closed.
A BiDi CDP bridge session was detached before another command.
A BiDi CDP bridge session was detached before another command.
Error message
Protocol error ({value}): Session closed. Most likely the page has been closed.
{value} is replaced by the value shown in your error message.
How this error happens
A detached CDP session cannot send commands. The underlying page may still be open, but that particular session is no longer usable.
The snippets use an open Puppeteer page named page.
const session = await page.createCDPSession();
await session.detach();
await session.send(
"Runtime.evaluate",
{
expression: "1 + 1",
}
);
How to fix
Use a live session, and detach it after all commands finish.
const session = await page.createCDPSession();
try {
const result = await session.send(
"Runtime.evaluate",
{
expression: "1 + 1",
}
);
console.log(result.result.value);
} finally {
await session.detach();
}
Things to keep in mind
A closed page or browser cannot supply a new live session. The browser must support CDP or a supported CDP bridge.