Target closed error in Puppeteer: pinpointing the closed page
0 reputation · 10 Oct 2020, 14:25 UTC
0 reputation · 10 Oct 2020, 14:25 UTC
Puppeteer throws a Target closed error when a Page instance is used after its underlying target has been closed. The error message contains no target identifier, making it difficult to determine which tab or frame was affected.
page.close() calls and unexpected browser crashes.Page to a new target.Should Puppeteer augment the Target closed error with the target ID or expose a dedicated API to reattach a Page to a new target after a closure?
Target closed error to the specific Page or target ID that caused it?Page instances that encounter this error?page.close() and an unexpected browser crash when the error is thrown?35775 reputation · 10 Oct 2020, 15:50 UTC
Capture page.target().id() before the target can close. Store that ID alongside the Page reference. When a Target closed error surfaces, match it against your stored map. The error itself carries no target identifier in current Puppeteer releases.
page.target().id() returns a string ID while the target is alive. After closure the Target object detaches and id() may throw or return undefined.Target closed with no targetId, page, or frame property.Page instance to a new target. Recovery requires creating a fresh page via browser.createPage() (or browser.newPage() in newer versions).const pageMap = new Map(); // targetId -> { page, metadata }
async function trackPage(page, metadata = {}) {
const targetId = page.target().id();
pageMap.set(targetId, { page, metadata, createdAt: Date.now() });
page.on('close', () => pageMap.delete(targetId));
return targetId;
}
try {
await page.goto(url);
} catch (err) {
if (err.message.includes('Target closed')) {
for (const [tid, entry] of pageMap) {
if (entry.page === page) {
console.error('Target closed:', tid, entry.metadata);
break;
}
}
// Recovery: create a new page, re-navigate, re-authenticate
const newPage = await browser.newPage();
await trackPage(newPage, metadata);
await newPage.goto(url);
// re-apply cookies, auth headers, viewport, etc.
} else {
throw err;
}
}
No flag exists on the error. You must instrument the code paths:
page.close() calls in a helper that marks the target ID as "expected" before closing.browser.on('disconnected') to flag unexpected browser-level terminations.page.close().catch(() => {}) on the failed page before creating a replacement to avoid target leaks.console.log(page.target().id()) immediately after page creation in your installed Puppeteer version to confirm the return type.page.close(), then use page) and catch the error; verify the message is exactly Target closed with no extra properties.Which Puppeteer major version are you targeting? The Target API surface and error class hierarchy shifted across major releases (for example browser.createPage() versus browser.newPage()), which affects both the catch logic and the recovery snippet above.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.