How to handle page redirections in Puppeteer?

In Puppeteer, handling page redirections is a common task that can be accomplished using the waitForNavigation function. The waitForNavigation function waits for the page to be fully loaded after the redirection.

Here is how you can handle page redirections in Puppeteer using JavaScript:

const puppeteer = require('puppeteer');

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

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

    // Set the navigation promise
    const navigationPromise = page.waitForNavigation();

    // Perform some action that causes a redirection
    await page.click('a.some-link');

    // Wait for the page to be fully loaded after the redirection
    await navigationPromise;

    // Now you can perform actions on the new page
    await page.screenshot({ path: 'screenshot.png' });

    await browser.close();
}

run();

In the above example, waitForNavigation is called before the action that will trigger the redirection. This is because page.click() might resolve immediately after the click event and before the navigation has finished. By setting the navigation promise before the click, you ensure that you can await the promise after the click.

waitForNavigation also takes an optional options object which can have the following properties:

  • waitUntil: this option specifies the event to wait for, it can be load (default value), domcontentloaded, networkidle0, networkidle2.
  • timeout: maximum time to wait for in milliseconds (default to 30000).

Here is an example of how to use these options:

await page.waitForNavigation({ waitUntil: 'networkidle0' });

This will wait until there are no more than 0 network connections for at least 500 ms.

Remember that handling page redirections is an important part of web scraping or automating browsers, and Puppeteer provides you with the necessary tools to do it relatively easily.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon