What is the Puppeteer method to take a full-page screenshot?
Davide S
davide s profile pic

The Puppeteer method to take a full-page screenshot ispage.screenshot(). Here's a detailed explanation: 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. Taking a full-page screenshot usingpage.screenshot(): To take a full-page screenshot in Puppeteer, you can use thepage.screenshot() method and specify the path and options for the screenshot.

1
2
3

   await page.screenshot({ path: 'fullpage.png', fullPage: true });
   

In this example,page.screenshot() is called with an options object. Thepath property is used to specify the file path where the screenshot should be saved. ThefullPage property is set totrue to capture the entire page. By default, Puppeteer takes screenshots of only the viewport, but settingfullPage totrue enables capturing the entire page. You can adjust thepath and other options based on your requirements. 3. Capturing the full-page screenshot with custom dimensions: If you want to capture the full page with specific dimensions, you can set theviewport property of the page before taking the screenshot.

1
2
3
4

   await page.setViewport({ width: 1280, height: 800 });
   await page.screenshot({ path: 'fullpage.png', fullPage: true });
   

In this code snippet,page.setViewport() is used to set the viewport size to a width of 1280 pixels and a height of 800 pixels. Then,page.screenshot() is called to capture the full page with the specified dimensions. By following these steps, you can take a full-page screenshot in Puppeteer. By using thepage.screenshot() method with thefullPage option set totrue, you can capture the entire content of the page. Additionally, you can customize the screenshot by adjusting options such as the file path, viewport dimensions, or other properties as needed. This functionality allows you to automate the capturing of full-page screenshots for testing, monitoring, or visual diffing purposes.