How to take a screenshot of a webpage with Puppeteer?

To take a screenshot of a webpage using Puppeteer, you can follow these steps:

  1. Install Puppeteer in your project by running the following npm command:
npm install puppeteer
  1. Create a new JavaScript file and require Puppeteer at the beginning of the file:
const puppeteer = require('puppeteer');
  1. Write the code to launch a new browser instance, open a new page, navigate to the desired URL, and take a screenshot:
(async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://www.example.com'); await page.screenshot({ path: 'screenshot.png' }); await browser.close(); })();
  1. Save the file and run it using Node.js. A new browser instance will open, navigate to the specified URL, take a screenshot of the webpage, and save it as "screenshot.png" in the same directory where the script is located.

You can also customize the screenshot by specifying additional options when calling the page.screenshot() method. For example, you can set the viewport size, quality, and format of the screenshot. For more information, you can refer to the Puppeteer documentation: https://pptr.dev/#?product=Puppeteer&version=v22.0.3&show=api-pagescreenshotoptions