How can I detect when a page has finished loading resources in Puppeteer?
Richard W
richard w profile pic

Detecting when a page has finished loading resources in Puppeteer involves using thepage.waitForNavigation() orpage.waitForSelector() method to wait for specific events or elements on the page. Here's a detailed explanation of how to detect when a page has finished loading resources: 1. Launching a new browser instance and creating a new page:

1
2
3
4
5
6
7
8
9
10
11
12
13

   const puppeteer = require('puppeteer');

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

     // Perform actions with the page here

     // Close the browser
     await browser.close();
   })();
   

This code sets up a basic Puppeteer script. It launches a new headless browser instance and creates a new page to work with. 2. Usingpage.waitForNavigation() to wait for a navigation event: If you want to wait for a navigation event (e.g., page load, page redirect), you can use thepage.waitForNavigation() method.

1
2
3
4
5

   await page.goto('https://example.com');
   await page.waitForNavigation();
   console.log('Page finished loading resources');
   

In this example, after navigating to a page usingpage.goto(),page.waitForNavigation() is called to wait for the page to finish loading resources. The script will continue execution once the navigation event occurs. 3. Usingpage.waitForSelector() to wait for a specific element: Alternatively, if you want to wait for a specific element to appear on the page, you can use thepage.waitForSelector() method.

1
2
3
4
5

   await page.goto('https://example.com');
   await page.waitForSelector('YOUR_SELECTOR');
   console.log('Page finished loading resources');
   

In this code snippet, after navigating to a page usingpage.goto(),page.waitForSelector() is called to wait until an element matching the specified selector (YOUR_SELECTOR) appears on the page. The script will continue execution once the element is found. 4. Usingpage.waitForFunction() to wait for a custom condition: If you have a custom condition or event you want to wait for, you can use thepage.waitForFunction() method to wait until a specified JavaScript expression returns a truthy value.

1
2
3
4
5
6
7
8

   await page.goto('https://example.com');
   await page.waitForFunction(() => {
     // Custom condition to check if resources have finished loading
     return performance.getEntriesByType('resource').every((entry) => entry.responseEnd > 0);
   });
   console.log('Page finished loading resources');
   

In this example, after navigating to a page usingpage.goto(),page.waitForFunction() is used to wait until a custom condition is met. The condition checks if all resources have finished loading by examining theresponseEnd property of each resource entry in the performance timeline. By following these steps, you can detect when a page has finished loading resources in Puppeteer. Whether you want to wait for a navigation event, a specific element, or a custom condition, Puppeteer provides methods likepage.waitForNavigation(),page.waitForSelector(), andpage.waitForFunction() to handle different scenarios and synchronize your script with the page's resource loading process.