Skip to content
Back to blog

Playwright waitForLoadState(): How to Use It (With Examples)

Table of contents

Key takeaways:

  • waitForLoadState() pauses execution until a page reaches a specific load state: load, domcontentloaded, or networkidle.
  • load is the default state, but you can choose a different state and set a custom timeout when needed.
  • A completed load state doesn’t always mean dynamic content is ready, so waiting for a specific element is often more reliable.
  • Playwright already uses auto-waiting for many actions, so you usually only need an explicit wait in specific navigation, pop-up, or scraping scenarios.

Playwright’s waitForLoadState() method lets you wait until a page reaches a specific stage of loading before continuing your script. It’s useful when an action triggers navigation, opens a new page, or starts loading content that your next step depends on.

For example, you can wait for the standard load event after clicking a link:

await page.getByRole('link', { name: 'Dashboard' }).click();
await page.waitForLoadState('load');
 
console.log('Page loaded');

However, waitForLoadState() isn’t something you need after every action. Playwright’s auto-waiting already handles many waits, and a page can reach the load state while JavaScript or dynamic content is still loading. The right waiting method depends on what your script actually needs before it continues.

What is waitForLoadState() in Playwright?

waitForLoadState() is a Playwright method that pauses your script until the current page reaches a specific loading state. You can wait for the standard load event, the earlier domcontentloaded state, or networkidle.

It’s useful when your script needs to continue only after navigation has progressed to a certain point. For example, you may need to wait after a reload, redirect, or when a click opens a new tab or pop-up.

await page.getByRole('link', { name: 'Pricing' }).click();
await page.waitForLoadState('domcontentloaded');

Here, Playwright continues once the browser has parsed the new page’s HTML and fired the DOMContentLoaded event, even if images are still loading.

However, reaching a load state doesn’t always mean the page is fully loaded and ready for your next action. Modern websites often fetch data, render components, or update the interface after the initial document has loaded. Background requests can also continue long after the load event fires.

For this reason, waitForLoadState() works best when the load state itself matters. If your script needs a specific button, product list, or other dynamic content, waiting for that element directly is usually more reliable.

Playwright also includes auto-waiting for many actions, so you don’t need to add waitForLoadState() after every click or navigation.

If you’re still choosing an automation library, our Playwright vs. Puppeteer comparison covers their browser support, language support, and APIs in more detail.

Playwright waitForLoadState() syntax and basic example

The basic waitForLoadState() syntax looks like this:

await page.waitForLoadState(state, { timeout });

It accepts two main parameters:

  • state defines which load state Playwright should wait for. You can use 'load', 'domcontentloaded', or 'networkidle'. If you leave it out, Playwright waits for load.
  • timeout sets how long Playwright should wait before throwing an error. The value is in milliseconds, and 0 disables it. In a standalone script, the default timeout is 30 seconds. In Playwright Test, the method has no limit of its own, and the 30-second test timeout applies instead.

For example, this waits up to 10 seconds for the DOMContentLoaded event:

await page.waitForLoadState('domcontentloaded', {
  timeout: 10000,
});

You can also use it after an action that triggers navigation:

await page.getByRole('link', { name: 'Account' }).click();
await page.waitForLoadState('load');
 
console.log('Account page loaded');

However, you usually don’t need to add waitForLoadState() immediately after page.goto(). By default, page.goto() already waits for the page’s load event before it resolves:

await page.goto('https://example.com');
// The load event has already fired here.

If you need a different loading point during navigation, you can pass the waitUntil option to page.goto() instead. We’ll compare the two approaches later in the article.

Understanding Playwright’s three load states

Playwright gives waitForLoadState() three states to choose from: load, domcontentloaded, and networkidle. They all wait for different points in the page-loading process, so the best option depends on what your script actually needs.

A useful rule is to choose the earliest state your next step actually needs. If you only need the DOM, domcontentloaded may be enough. If your script depends on images, stylesheets, or other initial resources, use load.

Don’t choose networkidle just because it sounds more complete: background requests can continue after the page is usable, and network idle doesn’t guarantee that a specific component has been rendered. For dynamic pages, a locator, URL change, or API response usually gives you a clearer signal.

load

load is the default state for waitForLoadState(). It waits until the browser fires the page’s load event, which happens after the document and dependent resources such as stylesheets, scripts, images, and iframes have loaded.

Use it when your next step depends on the page reaching the regular browser load point.

await page.waitForLoadState('load');

Keep in mind that load still doesn’t guarantee that every piece of dynamic content is ready. A page can continue fetching data or rendering components after this event fires.

domcontentloaded

domcontentloaded waits for the DOMContentLoaded event. This happens earlier than load: the browser has parsed the HTML and built the DOM, but resources such as images and some other page assets may still be loading.

That makes it useful when your script only needs the page structure and doesn’t depend on every resource being ready.

await page.waitForLoadState('domcontentloaded');
 
const title = await page.locator('h1').textContent();

Compared with load, this can let your script continue sooner. However, it still won’t wait for content that JavaScript fetches and renders afterward.

networkidle

networkidle waits until the page has no active network connections for at least 500 milliseconds.

await page.waitForLoadState('networkidle');

This may sound like the safest way to wait for a fully loaded page, but Playwright discourages using networkidle for testing. Many websites keep background requests open for analytics, polling, ads, notifications, or other services, so the page may never stay idle long enough.

Even when networkidle does resolve, it doesn’t prove that the specific content your test needs is ready. For tests, it’s usually better to wait for a visible element or use a web assertion that directly checks the expected result.

Use networkidle only when the absence of network requests is genuinely relevant to what your script is doing.

When and how to use waitForLoadState()

You don’t need waitForLoadState() after every navigation. Playwright’s auto-waiting already covers many actions. An explicit wait is most useful when your next step depends on a particular loading stage, especially after reloads, redirects, or when working with a newly opened page.

Waiting after navigation or page reload

You don’t always need a separate waitForLoadState() after navigation or a reload because Playwright’s navigation methods already wait for a load state.

For example, if you reload a page but only need the DOM to be available before continuing, set waitUntil directly:

await page.reload({
  waitUntil: 'domcontentloaded',
});
 
const heading = await page.locator('h1').textContent();

This is more direct than calling page.reload() and then waiting for domcontentloaded, since page.reload() waits for load by default, and the DOMContentLoaded event has already fired by then.

An explicit wait with waitForLoadState() is more useful when navigation has already been committed, and you need to wait for a later loading stage:

await page.goto('https://example.com', {
  waitUntil: 'commit',
});
 
await page.waitForLoadState('load');

Here, page.goto() returns once the network response is received and the document starts loading. waitForLoadState('load') then waits until the regular load event fires.

Waiting for a new tab or pop-up

waitForLoadState() is especially useful with a new page or pop-up. First, wait for the pop-up itself, then wait for that page to reach the required load state.

const [popup] = await Promise.all([
  page.waitForEvent('popup'),
  page.getByRole('link', { name: 'Open report' }).click(),
]);
 
await popup.waitForLoadState('load');
 
console.log(await popup.title());

Starting waitForEvent('popup') before the click prevents you from missing the event. Once Playwright returns the new Page object, popup.waitForLoadState() lets you wait on that page rather than the original one.

Handling dynamically loaded content

Load states become less useful when the content you need appears after the initial page load. A product grid, search results, or prices may come from network requests that finish after the load or DOMContentLoaded event.

For web scraping, wait for the specific content instead:

await page.goto('https://example.com/products');
 
const products = page.locator('.product-card');
await products.first().waitFor({ state: 'visible' });
 
const names = await products.locator('.product-name').allTextContents();
console.log(names);

This matters in scraping workflows, where Playwright often runs in a headless browser. Sites may render prices, product lists, or search results after the initial document is ready, so reading them as soon as the page loads can return an empty list.

If the data comes from a known API endpoint, you can also wait for that response before reading the element. The response confirms that the data arrived, and the locator confirms that the page rendered it.

Scrape Dynamic Product Pages with Playwright in 2026

Start scraping

waitForLoadState() vs. other Playwright waiting methods

waitForLoadState() is only one way to control timing in Playwright. In many cases, waiting for a URL, element, or network response gives you a clearer signal that the page is actually ready for your next step.

waitForLoadState() vs. waitUntil

Both can use load states, but they are used at different points.

waitUntil is an option you pass directly to navigation methods such as page.goto(). It tells Playwright when that navigation should be considered complete:

await page.goto('https://example.com', {
  waitUntil: 'domcontentloaded',
});

waitForLoadState(), on the other hand, is a separate wait you call after navigation has been committed:

await page.waitForLoadState('domcontentloaded');

Use waitUntil when you already know which state you want during navigation. Use waitForLoadState() when you need to wait for a page that was opened or changed elsewhere in your script.

waitForLoadState() vs. waitForURL()

Use waitForURL() when the destination matters more than the page’s loading stage. For example, a login may redirect through several URLs before reaching the account page:

await page.getByRole('button', { name: 'Log in' }).click();
await page.waitForURL('**/account');

Here, waiting for load wouldn’t tell you whether the browser reached the correct page. waitForURL() waits until the main frame matches the URL you expect, and can also accept a waitUntil option if you need both checks. It also replaces page.waitForNavigation(), which Playwright now marks as deprecated.

waitForLoadState() vs. locator.waitFor()

For dynamic content, locator.waitFor() is often the better choice because it waits for the exact element your script needs.

const results = page.locator('.search-results');
await results.waitFor({ state: 'visible' });

A page may fire its load event before search results, prices, or other JavaScript-rendered content appears. Waiting for the element itself avoids guessing when the page is ready. In Playwright Test, a web-first assertion such as await expect(results).toBeVisible() does the same job and is what Playwright’s best practices recommend.

If your next step depends on data from a specific API request rather than an element, you can use page.waitForResponse() instead:

const responsePromise = page.waitForResponse('**/api/products');
 
await page.getByRole('button', { name: 'Search' }).click();
await responsePromise;

In short, use waitForLoadState() for page-level loading, waitForURL() for navigation destinations, and locator or response waits when your script depends on something more specific.

Common waitForLoadState() errors and how to fix them

Most waitForLoadState() problems come from waiting for the wrong signal. A page can reach a load state too early, or keep network activity running long after the content you need is ready.

waitForLoadState() timeout errors

A timeout usually means the page never reached the state you requested within the configured timeframe. The cause depends on the state you’re waiting for. Slow page resources can delay load, while ongoing background requests can stop the page from ever reaching network idle. A custom timeout that’s too short can also cause the wait to fail.

You can set a timeout directly:

await page.waitForLoadState('load', {
  timeout: 10000,
});

In a standalone script, waitForLoadState() stops after 30 seconds by default, and page.setDefaultNavigationTimeout() changes that limit. In Playwright Test, the method has no timeout of its own, but the test timeout (30 seconds by default) still stops the test.

When a standalone script hits that limit, the error reads like this:

page.waitForLoadState: Timeout 30000ms exceeded.

Before increasing the limit, check whether you actually need that load state. Waiting for a specific element or response may solve the problem more reliably.

waitForLoadState() returns immediately

This usually isn’t an error. If the page has already reached the requested load state, Playwright resolves waitForLoadState() immediately.

For example:

await page.goto('https://example.com');
await page.waitForLoadState('load');

The second line may return right away because page.goto() already waits for load by default.

If you need to wait for something that happens afterward, use a more specific condition such as waitForURL() or locator.waitFor().

networkidle never finishes

networkidle requires the page to have no active network connections for at least 500 ms. Pages that continuously send analytics, polling, ads, or background requests may never reach that point.

Instead of waiting for network idle, wait for the result your script needs:

await page.locator('.results').waitFor({ state: 'visible' });

You can also use waitForResponse() when a specific API request matters. Playwright’s docs mark networkidle as discouraged for testing and recommend web assertions instead.

Page loaded, but elements are missing

The load event only tells you that the page and its main resources have loaded. JavaScript can still fetch data and render dynamic content afterward.

If your script needs a specific element, wait for that element directly:

const products = page.locator('.product-card');
 
await products.first().waitFor({
  state: 'visible',
});

This is usually more reliable than adding another page-level wait because your script continues only when the content it actually needs appears.

Conclusion

waitForLoadState() is useful when you need Playwright to wait for a specific stage of page loading. Use load for the regular browser load event, domcontentloaded when you only need the DOM ready, and networkidle only in cases where network inactivity itself matters.

Still, a load state isn’t always the best signal. If your script depends on a specific URL, element, or API response, use waitForURL(), locator.waitFor(), or waitForResponse() instead.

The most reliable approach is to wait for the condition your next step actually depends on, rather than adding a generic wait by default.

FAQ

How do I wait for a page to fully load in Playwright?

If you want to wait for the regular browser load event, use:

await page.waitForLoadState('load');

Keep in mind that this doesn’t mean the page is fully loaded: dynamic content may still be on its way. If your script depends on a specific element or API response, wait for that directly instead.

Does Playwright automatically wait for pages to load?

Yes, in many cases. Playwright has built-in auto-waiting, and navigation methods such as page.goto() already wait for the page to reach a load state by default.

That means you usually don’t need to add waitForLoadState() after every navigation or click. Use it when you specifically need to wait for load, domcontentloaded, or networkidle.

Does waitForLoadState() wait for JavaScript to finish?

No. waitForLoadState() waits for a browser load state, not for all JavaScript execution to stop.

JavaScript can continue running after load or DOMContentLoaded, and many pages keep sending network requests or rendering content in the background. If you need something produced by JavaScript, wait for the relevant element, URL change, or network response instead.

Learn more

Related articles