Puppeteer is a Node.js library that provides a high-level API to control Google Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run non-headless.
Installation
To install Puppeteer, you need to have Node.js installed on your machine. If you haven't installed Node.js yet, you can download it from the official website.
After you have Node.js installed, you can install Puppeteer using npm (Node Package Manager) which comes with Node.js. Open your terminal or command prompt and type the following command:
npm i puppeteer
This command will install the latest version of Puppeteer. If you want to install a specific version, you can do so by appending @<version>
to the package name, like so:
npm i puppeteer@1.4.0
By default, Puppeteer downloads and uses a specific version of Chromium so its API is guaranteed to work out of the box. To skip the Chromium download when you install puppeteer, you can set the environment variable PUPPETEER_SKIP_CHROMIUM_DOWNLOAD
. Note that puppeteer will not be able to run unless you either download Chromium (using npm run download
command) or set the executablePath
option to a path to a local Chromium or Chrome executable.
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true npm i puppeteer
Usage
After the installation is complete, you can require Puppeteer in your script and start using it. Here is a simple example of how to use Puppeteer to take a screenshot of a webpage:
const puppeteer = require('puppeteer');
async function run() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
browser.close();
}
run();
This script will launch a new browser instance, open a new page, navigate to 'https://example.com', take a screenshot and save it as 'example.png', and finally close the browser instance.