Main Website
Web Scraping
Web Scraping
Updated on
May 21, 2024

'networkidle0' vs. 'networkidle2' in Puppetteer

Puppeteer, a Node library developed by Google, offers powerful capabilities for web automation tasks. In this article, we’ll explore the nuances between Puppeteer’s networkidle0 and networkidle2 functions. These functions play a vital role in determining when a webpage is fully loaded, but understanding their differences is key to optimizing your automation scripts.  We’ll explore their core functions, understand how they differ and discuss when to use each effectively. Let’s get started!

Defining the core functions

In Puppeteer, networkidle0 and networkidle2 are essential functions used to determine the loading status of web pages. These functions play a crucial role in synchronizing automation tasks, ensuring that actions are performed only when the desired loading conditions are met. Let’s delve into the specifics of each function to understand their significance in Puppeteer scripting.

What is networkidle0?

In Puppeteer, networkidle0 is a function used to wait until there are no more than 0 network connections for at least 500 milliseconds. This signifies that the page has finished loading and there are no pending network requests.

When using networkidle0, Puppeteer considers a page to be fully loaded when there are no ongoing network connections, ensuring that all resources, including asynchronous Javascript, have been fetched and processed. Below is the code snippet depicting the use of networkidle0 function:


const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Navigate to a webpage
  await page.goto('https://example.com');

  // Wait for the page to reach network idle state
  await page.waitForNavigation({ waitUntil: 'networkidle0' });

  // Perform actions on the fully loaded page

  await browser.close();
})();

To ensure that the page is fully loaded before performing any actions, we use page.waitForNavigation() with the waitUntil option set to 'networkidle0'. This instructs Puppeteer to wait until there are no more than 0 network connections for at least 500 milliseconds before proceeding.

What is networkidle2?

In Puppeteer, networkidle2 is a function used to wait until there are no more than 2 network connections for at least 500 milliseconds. This indicates that the page is considered fully loaded when there are at most 2 active network connections.

Unlike networkidle0, which waits for all network connections to settle, networkidle2 allows for a maximum of 2 ongoing connections, providing a balance between speed and completeness in page loading. Below is the code snippet depicting the use of networkidle2 function:


const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Navigate to a webpage
  await page.goto('https://example.com');

  // Wait for the page to reach network idle state
  await page.waitForNavigation({ waitUntil: 'networkidle2' });

  // Perform actions on the fully loaded page

  await browser.close();
})();

To ensure that the page is fully loaded before proceeding, we use page.waitForNavigation() with the waitUntil option set to 'networkidle2'. This instructs Puppeteer to wait until there are no more than 2 network connections for at least 500 milliseconds before continuing. Once the page reaches the networkidle2 state, we can safely execute our desired actions on the fully loaded page. Finally, we close the browser instance using browser.close().

Differences between networkidle0 and networkidle2

Understanding the disparities between networkidle0 and networkidle2 is crucial for effectively utilizing Puppeteer in web automation tasks. Let’s delve into the key differences between these two functions:

Resource tolerance

  • networkidle0: As discussed earlier, this function waits until there are no more than 0 network connections for at least 500 milliseconds. It ensures that all resources, including asynchronous Javascript, have been fetched and processed before proceeding with automation tasks.
  • networkidle2: This function allows for a maximum of 2 ongoing network connections. This tolerance level accommodates pages with dynamic content or ongoing network activity, allowing for quicker execution of automation tasks without waiting for every single resource to load.

Usage in dynamic content scenarios

  • networkidle0: It is best suited for scenarios where pages contain extensive dynamic content or heavy resource requirements. It ensures thorough page loading by waiting for all network connections to settle, making it ideal for tasks that require complete page rendering.
  • networkidle2: It is particularly useful for pages with dynamic content that may involve ongoing network requests or periodic updates. By allowing for a small number of ongoing network connections, networkidle2 strikes a balance between completeness and efficiency in page loading. 

Handling asynchronous tasks

  • networkidle0: It ensures that all asynchronous Javascript and network requests have been processed before proceeding with automation tasks. This function is essential for tasks that depend on the availability of fully loaded resources, such as data extraction from dynamic elements.
  • networkidle2: It provides a more relaxed threshold for page loading, allowing automation tasks to proceed once the most critical resources are available. It is suitable for scenarios where waiting for every single resource to load may result in unnecessary delays.

When should you use networkidle0?

networkidle0 in Puppeteer is best suited for scenarios where web pages rely heavily on asynchronous Javascript requests, such as Single Page Applications (SPAs) or websites that utilize fetch requests. Let’s explore in detail the specific scenarios in which networkidle0 should be used:

  • Single Page Applications: SPAs heavily rely on Javascript to dynamically load content without full page refreshes. These applications often make extensive use of asynchronous requests to fetch data from the server. networkidle0 ensures that all asynchronous Javascript and network requests have been processed before proceeding with automation tasks, making it ideal for testing and scraping SPAs.
  • Pages with Fetch Requests: Websites that utilize fetch requests or similar mechanisms to retrieve data asynchronously often close network connections once the data transfer is complete. networkidle0 is well-suited for such scenarios as it waits until there are no more than 0 network connections for at least 500 milliseconds, ensuring that all fetch requests and network activity have ceased before proceeding.
  • Dynamic Content Loading: networkidle0 is also beneficial for pages with dynamic content loading mechanisms, such as lazy loading images or infinite scroll. It ensures that all dynamically loaded content, including resources fetched asynchronously, has been fully loaded before proceeding with automation tasks.

When should you use networkidle2?

networkidle2 in Puppeteer is best-suited for scenarios where web pages utilize polling techniques or maintain long-lived network connections. Let’s explore the specific scenarios in which networkidle2 should be used:

  • Polling Techniques: When a web page employs polling, it continuously sends data over time without closing network connections. In such cases, networkidle2 allows for a maximum of 2 ongoing network connections. This tolerance level ensures that automation tasks can proceed once the initial data transfer is complete, without waiting for all network connections to settle.

For example, consider a stock market tracking website that updates stock prices every 30 seconds using polling. If you are automating a task to scrape data from this website using Puppeteer, you can utilize networkidle2 to optimize your script’s execution. Instead of waiting for all network connections to cease, which could introduce unnecessary delays due to the continuous  polling activity, you can instruct Puppeteer to proceed with the automation task once the critical data has been received and processed. This ensures efficient execution of the script while accommodating the ongoing network activity associated with polling.

Conclusion

Understanding the distinctions between networkidle0 and networkidle2 in Puppeteer is crucial for optimizing web automation tasks. networkidle0 is ideal for SPAs and pages with extensive asynchronous requests, ensuring thorough resource loading. Conversely, networkidle2 suits scenarios with long-lived connections like polling, allowing tasks to proceed once critical resources are available. By leveraging these functions appropriately, you can enhance the effectiveness of your Puppeteer scripts for various automation tasks.

waitForNavigation in Puppeteer: Basic and Advanced Configuration

Wait For Page to Load in Puppeteer: 4 Methods Compared