Puppeteer for JavaScript (Node.js): Complete Guide with Examples
\n\n1. What It Is and Why It's Needed
\nPuppeteer is a library for Node.js that provides a high-level API for controlling the Chrome (or Chromium) browser via the DevTools Protocol. By default, Puppeteer runs in headless mode (without a graphical interface), making it an ideal tool for automating web tasks on the server side. However, if necessary, you can also launch a full browser with an interface for debugging.
\nThe main use cases for Puppeteer include: web scraping (extracting data from dynamic websites that use JavaScript), automated web application testing (e2e tests), generating screenshots and PDFs of pages, emulating user actions (clicks, form filling, navigation), and web page performance monitoring. Unlike simple HTTP requests, Puppeteer executes JavaScript on the page, allowing you to retrieve dynamically generated content.
\nThe library is actively maintained by the Google team and is used in production by thousands of companies. Puppeteer provides full control over the browser: from managing tabs and intercepting network requests to emulating mobile devices and geolocation. It is a versatile tool that replaces a whole set of utilities for working with web content.
\n2. Installation
\nTo install Puppeteer via npm (Node Package Manager), run the following command in the terminal at the root of your project:
\nnpm install puppeteer\n This command will download the library itself and automatically download a compatible version of Chromium (about 300-400 MB). If you want to use an already installed Chrome/Chromium on your system, install puppeteer-core:
npm install puppeteer-core\n When using puppeteer-core, you will need to specify the path to the browser executable when launching. The library requires Node.js version 18.0.0 or higher (LTS version is recommended).
3. Quick Start — Minimal Working Example
\nCreate a file example.js and add the following code, which takes a screenshot of a web page:
const puppeteer = require('puppeteer');\n\n(async () => {\n // Launch the browser\n const browser = await puppeteer.launch();\n // Open a new page\n const page = await browser.newPage();\n // Navigate to the website\n await page.goto('https://example.com');\n // Take a screenshot\n await page.screenshot({ path: 'example.png' });\n // Close the browser\n await browser.close();\n})();\n Run the script with the command node example.js. A file example.png with a screenshot of the example.com page will appear in the folder. This is a minimal but fully working example of browser automation.
4. Main Methods/Functions/Classes
\nBelow are 8 key Puppeteer methods and classes with descriptions and code examples.
\n\n4.1. puppeteer.launch()
\n Signature: puppeteer.launch([options]) → Promise<Browser>
What it does: Launches a Chromium/Chrome browser instance. Accepts an options object (headless, slowMo, args, etc.). Returns a Browser object.
\nconst browser = await puppeteer.launch({\n headless: false, // Show the browser\n slowMo: 100, // Slow down actions (ms)\n a