How to get a list of all links on a page using Puppeteer?

You can retrieve a list of all links on a page using Puppeteer by following these steps:

  1. Install Puppeteer using npm if you haven't already:
npm install puppeteer
  1. Use the following code snippet to launch a headless browser, navigate to a webpage, and extract all links from the page:
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://example.com'); // Replace 'https://example.com' with the URL of the webpage you want to extract links from // Extract all links on the page const links = await page.evaluate(() => { const allLinks = document.querySelectorAll('a'); const linksArray = Array.from(allLinks); return linksArray.map(link => link.href); }); console.log(links); await browser.close(); })();
  1. Run the script using Node.js:
node your_script_name.js

This code snippet uses Puppeteer to launch a headless browser, navigate to a specified webpage, and extract all links (URLs) present on the page. The extracted links are then logged to the console.