Web scraping
How do I wait for a page to load in Puppeteer?
Give goto a waitUntil value that fits the page, then wait for the specific element or condition your script needs, because "loaded" rarely means "your data is there".
The answer
page.goto(url, { waitUntil }) resolves when a browser event fires. "load" is the default and waits for the load event, "domcontentloaded" fires earlier once the HTML is parsed, "networkidle2" waits until no more than two connections are open for half a second, and "networkidle0" waits for none. None of these knows about the content you want. The dependable pattern is a quick navigation event followed by an explicit wait for your target with waitForSelector, waitForFunction or a locator.
A pattern that works on most sites
const nav = await page.goto("https://jobs.example.io/search?q=rust", {
waitUntil: "domcontentloaded",
timeout: 45_000,
});
if (!nav || !nav.ok()) throw new Error(`HTTP ${nav?.status()}`);
await page.waitForSelector("ul.job-list > li", { timeout: 20_000 });
await page.waitForFunction(() => document.querySelectorAll("ul.job-list > li").length >= 10,
{ polling: 250, timeout: 20_000 });
const titles = await page.$$eval("ul.job-list > li h3", (els) => els.map((e) => e.textContent.trim()));
Starting with domcontentloaded avoids waiting for ads, analytics and fonts you do not need. The status check catches error pages early, and the two waits express exactly what "ready" means for this page: the list exists and holds enough rows.
When to use networkidle
The networkidle options suit pages that fetch data in a few bursts and then go quiet. They are a poor fit for sites with live chat, polling, analytics beacons or websockets, which never become idle, so goto runs until its timeout. If you use them, keep the timeout modest and catch the timeout error rather than letting it end the run. For a one-off check later in the flow, page.waitForNetworkIdle({ idleTime: 500 }) exists in current versions.
Clicks that trigger navigation
When a click loads a new page, start waiting before you click, otherwise the navigation can finish before the wait begins:
const [response] = await Promise.all([
page.waitForNavigation({ waitUntil: "domcontentloaded", timeout: 45_000 }),
page.click("a.next-page"),
]);
Single page apps often change the URL without a real navigation. There, wait for new content instead, for example a selector that only exists on the next view, or a response with page.waitForResponse that matches the API call the view makes.
Timeouts and defaults
The default timeout is 30 seconds for most waits. Set it once with page.setDefaultTimeout() and page.setDefaultNavigationTimeout() instead of repeating it. Treat timeouts as information, not just failures: log the URL, take a screenshot and save the HTML. Most of the time the cause is visible at a glance, such as a cookie banner, an error page or a challenge.
Waiting forever on a challenge
If a site answers with a Cloudflare check, a reCAPTCHA or a DataDome slider, your selector never appears and every wait times out. Detect the challenge explicitly: race your content selector against a selector for the widget (an iframe[src*="recaptcha"], a .cf-turnstile element or a captcha-delivery.com frame), and branch. On the challenge branch, solve it through the GateSolver API and continue; the Puppeteer integration has the full flow.
Common mistakes
- Fixed sleeps.
await new Promise(r => setTimeout(r, 5000))is slow when the page is fast and too short when it is slow. Wait on conditions. - Removed helpers.
page.waitForTimeoutandpage.waitForwere removed in recent versions. Old tutorials still show them. - Visible versus present.
waitForSelectorresolves when the element is in the DOM. Pass{ visible: true }if you need it on screen before clicking. - Waiting in the wrong frame. Content inside an iframe needs a wait on the frame object.
Measuring what a page really needs
If you are unsure which event or element to wait for, measure once. Load the page with request logging turned on (page.on("requestfinished", ...)) and note when the request carrying your data completes compared with domcontentloaded and load. You will often find that the data arrives from one API call a second after the DOM is ready, while the load event waits for a dozen tracking scripts. Waiting for that specific response, or the element it produces, can cut seconds from every page and makes the script independent of third-party scripts you do not control.
How this compares with Playwright and Selenium
Playwright's locators auto-wait for actions, which removes many explicit waits, and its goto accepts similar waitUntil values. Selenium relies on explicit waits with conditions; the companion question on waiting in Selenium covers it. For debugging timeouts in general, see debugging Puppeteer scripts.
Common questions
Which waitUntil value is fastest?
domcontentloaded usually resolves first because it does not wait for images, fonts or late scripts.
Why does networkidle0 time out?
The page keeps connections busy with polling, analytics or websockets, so the network never goes quiet.
What replaced page.waitForTimeout?
Use a condition wait instead. If you truly need a pause, a plain setTimeout promise does the same job.
How do I wait for an API response the page makes?
Use page.waitForResponse with a function that matches the response URL, then read its JSON if you need the data.
Does waitForSelector wait for the element to be visible?
Only if you pass visible: true. By default it resolves once the element exists in the DOM.
Keep reading
- PuppeteerThe Node.js library that drives Chrome and Chromium over the DevTools Protocol, popular for scraping, screenshots and PDFs.
- How do I debug Puppeteer scripts?Run headful with slowMo, forward page console logs, take screenshots on failure and log network responses.
- Web scraping with PuppeteerPuppeteer scraping beyond the basics: picking the right navigation wait, bulk extraction with $$eval, blocking heavy requests and capturing JSON.
- TimeoutConnect, read and total timeouts for HTTP calls, plus the separate deadline for an asynchronous task such as a CAPTCHA solve.