page.waitForSelector gives up after 30 seconds although the element is on the page
Fix it fast
Most common cause, 1 of 4The 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.
const holder = await page.waitForSelector('iframe[title="Secure card number"]');
const frame = await holder.contentFrame();
const field = await frame.waitForSelector('#card-number');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.
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.
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:
- 1The element lives inside an iframe
The 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 - 2The element is inside a shadow root
DevTools shows the node under a #shadow-root entry inside a host element - 3The visible option waits for an element that stays hidden
The call passes visible: true, and the node is in page.content() but styled display: none or visibility: hidden - 4The selector matches nothing on the page Puppeteer loaded
The 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` failedWaiting 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.
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.
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.
console.log(await field.evaluate((el) => el.outerHTML));Confirmed by raw.githubusercontent.com
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.
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.
await page.waitForSelector('checkout-widget >>> button.pay');Verify
The wait resolves, and clicking the returned handle works.
const pay = await page.waitForSelector('checkout-widget >>> button.pay');
await pay.click();Confirmed by pptr.dev
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.
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.
// 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.
node scrape.jsConfirmed by pptr.dev
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.
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.
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.
node scrape.jsConfirmed by pptr.dev
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.