Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

page.waitForSelector gives up after 30 seconds although the element is on the page

Your error

TimeoutError: Waiting for selector `#card-number` failed1 more line it can show

Fix it fast

Most common cause, 1 of 4

The element lives inside an iframe

Wait for the iframe element first, take the frame it holds with contentFrame(), and run the wait on that frame.

Fix
const holder = await page.waitForSelector('iframe[title="Secure card number"]');
const frame = await holder.contentFrame();
const field = await frame.waitForSelector('#card-number');
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

TimeoutError: Waiting for selector failed means page.waitForSelector found no match in the main frame within 30 seconds. An element you can see may sit inside an iframe (wait on its contentFrame()), inside a shadow root (join with >>>), stay hidden while visible: true waits, or be missing from the page Puppeteer loaded. Puppeteer 25 on Node.js 24.

  • Applies to Puppeteer 25.18, Node.js 24, Node.js 26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Catch the error and save what the browser had in front of it at that moment: a screenshot and the list of frames. Most causes below are visible in one of the two.

Run this first
try {
  await page.waitForSelector('#card-number');
} catch (error) {
  await page.screenshot({ path: 'at-timeout.png', fullPage: true });
  console.log(page.frames().map((f) => f.url()));
  throw error;
}

Then find the line you see in this list and open its fix:

  1. 1The element lives inside an iframeThe frame list holds more than one URL, for example a payment, captcha or embedded form provider, and the field sits inside that box on the screenshot
  2. 2The element is inside a shadow rootDevTools shows the node under a #shadow-root entry inside a host element
  3. 3The visible option waits for an element that stays hiddenThe call passes visible: true, and the node is in page.content() but styled display: none or visibility: hidden
  4. 4The selector matches nothing on the page Puppeteer loadedThe screenshot shows a cookie wall, a login form, an error or other markup, and no element that your selector could match

What does the error look like?

  • TimeoutError: Waiting for selector `#card-number` failed
  • Waiting failed: 30000ms exceeded

Cause 1 of 4

The element lives inside an iframe

You seeThe frame list holds more than one URL, for example a payment, captcha or embedded form provider, and the field sits inside that box on the screenshot

Check

Waiting for selector failed for a field you can see on screen can mean the field is inside an iframe, which page.waitForSelector never searches. In Puppeteer's source, page.waitForSelector simply hands the call to the main frame. A document loaded into an iframe is a separate frame, so its nodes never match a query made on the page.

Check
const urls = page.frames().map((f) => f.url());
console.log(urls);

Fix

Wait for the iframe element first, take the frame it holds with contentFrame(), and run the wait on that frame.

Fix
const holder = await page.waitForSelector('iframe[title="Secure card number"]');
const frame = await holder.contentFrame();
const field = await frame.waitForSelector('#card-number');

Verify

The wait on the frame resolves with an element handle instead of throwing.

Verify
console.log(await field.evaluate((el) => el.outerHTML));

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

The element is inside a shadow root

You seeDevTools shows the node under a #shadow-root entry inside a host element

Check

Waiting for selector failed for a node under #shadow-root means the selector stops at the shadow boundary. A plain CSS selector does not reach into a shadow tree. Puppeteer's selector guide adds deep combinators for this: >>> matches inside shadow roots at any depth and >>>> only inside the immediate one. They work only for open shadow roots.

Check
const host = await page.$('checkout-widget');
console.log(await host.evaluate((el) => el.shadowRoot !== null));

Fix

Join the host and the inner selector with the deep descendant combinator.

Fix
await page.waitForSelector('checkout-widget >>> button.pay');

Verify

The wait resolves, and clicking the returned handle works.

Verify
const pay = await page.waitForSelector('checkout-widget >>> button.pay');
await pay.click();

Confirmed by pptr.dev

Did this fix it?

Cause 3 of 4

The visible option waits for an element that stays hidden

You seeThe call passes visible: true, and the node is in page.content() but styled display: none or visibility: hidden

Check

Waiting for selector failed with visible: true while the node exists means Puppeteer is still waiting for it to become visible. With visible set, the API reference says the wait only ends once the node is in the DOM and has neither display: none nor visibility: hidden. A native input that the page hides with display: none and replaces with a styled element never meets it.

Check
const style = await page.$eval('#terms', (el) => {
  const s = getComputedStyle(el);
  return { display: s.display, visibility: s.visibility };
});
console.log(style);

Fix

Wait for the element people actually see and click, or drop the visible option when you only need the node to exist.

Fix
// the real checkbox is hidden, the label is what the user clicks
await page.waitForSelector('label[for="terms"]', { visible: true });
// or: the node only has to exist
await page.waitForSelector('#terms');

Verify

Run the script again; the wait ends as soon as the page is ready.

Verify
node scrape.js

Confirmed by pptr.dev

Did this fix it?

Cause 4 of 4

The selector matches nothing on the page Puppeteer loaded

You seeThe screenshot shows a cookie wall, a login form, an error or other markup, and no element that your selector could match

Check

Waiting for selector failed with nothing like the element on the screenshot means the selector does not match the page Puppeteer loaded. A selector copied from your own browser can depend on class names or a state that the automated session never reaches. Compare it with what the screenshot and the saved HTML really contain.

Check
import { writeFile } from 'node:fs/promises';
await writeFile('at-timeout.html', await page.content());

Fix

Handle the step that blocks the page first (accept the banner, sign in), then select by something stable. Puppeteer's text and ARIA selectors match visible text or the accessible name instead of generated class names.

Fix
await page.waitForSelector('::-p-aria(Pay now)');
await page.waitForSelector('button ::-p-text(Pay now)');

Verify

Run the script again. It gets past the wait, and the catch block writes no screenshot.

Verify
node scrape.js

Confirmed by pptr.dev

Did this fix it?

How do I stop it coming back?

  • Save a screenshot and the frame list in every catch block around a wait, so a failure explains itself.
  • Prefer text and ARIA selectors over class names produced by a CSS build.
  • Treat any third-party widget as an iframe or a shadow root until you have checked.
  • Use the visible option only for elements a person can actually see.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.