Skip to content
Puppeteer · error message

Puppeteer: Node is detached from document

RetryableElement handles & clicking

Last verified

The exact message
Error: Node is detached from document

What it means

You called click(), hover(), scrollIntoView() or a similar method on an ElementHandle whose DOM node has been removed from the page. A handle refers to one specific node; if the page re-renders that part of the DOM, the replacement looks the same but is a different node, and the old handle is orphaned. This is Puppeteer's version of a stale element reference.

It is common when scraping lists with pagination or infinite scroll, and on React, Vue or Angular sites that re-render components after data loads. Code that collects handles with page.$$() and then clicks each one in a loop is especially prone to it, because the first click often re-renders the list.

What is not wrong: the selector still matches. Querying it again will usually find the new node immediately.

What it looks like

// Fragile
const buttons = await page.$$('.row button.expand');
for (const b of buttons) await b.click();

// Robust: re-query each time by index
const count = await page.$$eval('.row button.expand', els => els.length);
for (let i = 0; i < count; i++) {
  await page.locator(`.row:nth-of-type(${i + 1}) button.expand`).click();
}

Why it happens

  • The framework re-rendered the component between querying the handle and using it.
  • Looping over page.$$() results while each click changes the list.
  • The handle was obtained before a navigation or a client-side route change.
  • A modal or popup was closed and re-opened, recreating its contents.

How to fix it

  1. Query the element right before acting on it, inside the loop rather than before it.
  2. Use page.click(selector) or page.locator(selector).click(), which resolve the selector at action time.
  3. For lists, extract stable identifiers (text, href, data-id) first and locate each item fresh by that identifier.
  4. Wait for the re-render to settle, for example by waiting for a loading indicator to disappear, before acting.

How to stop it recurring

Keep element handles short-lived and prefer selector-based or Locator API calls, which never hold onto a specific node. When iterating, iterate over data, not over handles. After any action that triggers a re-render, wait for a visible signal that the UI has settled before the next interaction.

Official documentation: pptr.dev/api/puppeteer.elementhandle. See all Puppeteer error messages.

Related errors

Definitions

Quick answers

What does "Node is detached from document" mean in Puppeteer?

You called click() , hover() , scrollIntoView() or a similar method on an ElementHandle whose DOM node has been removed from the page. A handle refers to one specific node; if the page re-renders that part of the DOM, the replacement looks the same but is a different node, and the old handle is orphaned. This is Puppeteer's version of a stale element reference.

How do I fix "Node is detached from document"?

1. Query the element right before acting on it, inside the loop rather than before it. 2. Use page.click(selector) or page.locator(selector).click(), which resolve the selector at action time. 3. For lists, extract stable identifiers (text, href, data-id) first and locate each item fresh by that identifier. 4. Wait for the re-render to settle, for example by waiting for a loading indicator to disappear, before acting.

Run browser automation you can watch and take over

Autonoly runs each job in a real cloud Chromium browser driven by Playwright. Timeouts and detached elements still happen; the difference is that you can watch the run live, see the step that failed, and take control of the browser to finish it.

Also see Live browser control.