How to handle redirects and follow them in Puppeteer?

To handle redirects and follow them in Puppeteer, you can use the waitForNavigation method after clicking on a link or submitting a form. Here is an example code snippet demonstrating how to handle redirects and follow them in Puppeteer:

const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://example.com'); // Click on a link or submit a form that will redirect the page await Promise.all([ page.waitForNavigation(), // Wait for the page to redirect page.click('a#redirect-link') // Click on a link with id "redirect-link" ]); console.log('Page redirected to: ' + page.url()); await browser.close(); })();

In this code snippet, we first navigate to a webpage using page.goto(). Then, we click on a link with the id "redirect-link" and use page.waitForNavigation() to wait for the page to redirect. Finally, we log the URL of the redirected page.