Node.js API for Chrome
Go to file
2024-02-16 07:57:36 +00:00
.devcontainer chore: create devcontainer.json (#9563) 2023-01-23 09:16:46 +00:00
.github ci: download latest Canary build (#11918) 2024-02-14 18:07:13 +00:00
.vscode refactor!: enable the new-headless mode by default (#11815) 2024-02-02 13:25:43 +01:00
docker test: fix the headless flag in the smoke test (#11894) 2024-02-12 12:03:51 +00:00
docs chore(deps-dev): Bump the dev-dependencies group with 13 updates (#11908) 2024-02-14 18:20:12 +00:00
examples build: fix EsLint rule and add fixer (#11826) 2024-02-05 10:26:37 +01:00
packages chore: wait for frames to be destroyed (#11919) 2024-02-15 09:22:23 +01:00
test test: enable cookie tests for Firefox (#11931) 2024-02-16 07:57:36 +00:00
test-d build: fix EsLint rule and add fixer (#11826) 2024-02-05 10:26:37 +01:00
tools build: auto-remove duplicate expectations (#11922) 2024-02-15 10:57:06 +01:00
website chore: release main (#11821) 2024-02-05 10:22:00 +00:00
.editorconfig EditorConfig: 2 space indent (#195) 2017-08-03 09:50:08 -07:00
.eslintignore chore: fix EsLint for .mjs (#11629) 2024-01-08 10:02:56 +01:00
.eslintrc.js chore: enable license header rule (#11855) 2024-02-06 18:07:30 +01:00
.eslintrc.types.cjs fix: remove unused imports (#8613) 2022-07-01 16:00:03 +02:00
.gitattributes chore(git): Fix line endings in text files (#4320) 2019-04-22 09:03:42 -07:00
.gitignore chore: fix EsLint for .mjs (#11629) 2024-01-08 10:02:56 +01:00
.mocharc.cjs chore: update license headers (#11563) 2024-01-03 10:11:33 +00:00
.npmrc fix: move CI npm config out of .npmrc (#6901) 2021-02-17 18:52:22 +01:00
.nvmrc ci: test node-version-file (#11737) 2024-01-24 11:27:35 +01:00
.prettierignore chore: fix EsLint for .mjs (#11629) 2024-01-08 10:02:56 +01:00
.prettierrc.cjs docs: improve docs (#9179) 2022-10-28 08:49:28 +02:00
.release-please-manifest.json chore(main): release ng-schematics 0.6.0 (#11820) 2024-02-05 13:03:02 +00:00
Herebyfile.mjs chore: disable eslint for herebyfile (#11741) 2024-01-24 12:28:37 +00:00
LICENSE chore: use https URL for license info (#6279) 2020-08-10 10:35:07 +02:00
package-lock.json chore(deps-dev): Bump the dev-dependencies group with 13 updates (#11908) 2024-02-14 18:20:12 +00:00
package.json chore(deps-dev): Bump the dev-dependencies group with 13 updates (#11908) 2024-02-14 18:20:12 +00:00
README.md refactor!: enable the new-headless mode by default (#11815) 2024-02-02 13:25:43 +01:00
release-please-config.json chore: manual bump of config (#10772) 2023-08-22 13:25:42 +00:00
SECURITY.md chore: Add a security policy (#9547) 2023-01-19 17:03:59 +01:00
tsconfig.base.json chore: update dependencies (#10785) 2023-08-28 13:01:52 +02:00
tsdoc.json chore: update license headers (#11563) 2024-01-03 10:11:33 +00:00
versions.js chore: release main (#11744) 2024-01-24 13:53:06 +00:00

Puppeteer

Build status npm puppeteer package

Guides | API | FAQ | Contributing | Troubleshooting

Puppeteer is a Node.js library which provides a high-level API to control Chrome/Chromium over the DevTools Protocol. Puppeteer runs in headless mode by default, but can be configured to run in full ("headful") Chrome/Chromium.

What can I do?

Most things that you can do manually in the browser can be done using Puppeteer! Here are a few examples to get you started:

  • Generate screenshots and PDFs of pages.
  • Crawl a SPA (Single-Page Application) and generate pre-rendered content (i.e. "SSR" (Server-Side Rendering)).
  • Automate form submission, UI testing, keyboard input, etc.
  • Create an automated testing environment using the latest JavaScript and browser features.
  • Capture a timeline trace of your site to help diagnose performance issues.
  • Test Chrome Extensions.

Getting Started

Installation

To use Puppeteer in your project, run:

npm i puppeteer
# or using yarn
yarn add puppeteer
# or using pnpm
pnpm i puppeteer

When you install Puppeteer, it automatically downloads a recent version of Chrome for Testing (~170MB macOS, ~282MB Linux, ~280MB Windows) and a chrome-headless-shell binary (starting with Puppeteer v21.6.0) that is guaranteed to work with Puppeteer. The browser is downloaded to the $HOME/.cache/puppeteer folder by default (starting with Puppeteer v19.0.0). See configuration for configuration options and environmental variables to control the download behavor.

If you deploy a project using Puppeteer to a hosting provider, such as Render or Heroku, you might need to reconfigure the location of the cache to be within your project folder (see an example below) because not all hosting providers include $HOME/.cache into the project's deployment.

For a version of Puppeteer without the browser installation, see puppeteer-core.

If used with TypeScript, the minimum supported TypeScript version is 4.7.4.

Configuration

Puppeteer uses several defaults that can be customized through configuration files.

For example, to change the default cache directory Puppeteer uses to install browsers, you can add a .puppeteerrc.cjs (or puppeteer.config.cjs) at the root of your application with the contents

const {join} = require('path');

/**
 * @type {import("puppeteer").Configuration}
 */
module.exports = {
  // Changes the cache location for Puppeteer.
  cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
};

After adding the configuration file, you will need to remove and reinstall puppeteer for it to take effect.

See the configuration guide for more information.

puppeteer-core

For every release since v1.7.0 we publish two packages:

puppeteer is a product for browser automation. When installed, it downloads a version of Chrome, which it then drives using puppeteer-core. Being an end-user product, puppeteer automates several workflows using reasonable defaults that can be customized.

puppeteer-core is a library to help drive anything that supports DevTools protocol. Being a library, puppeteer-core is fully driven through its programmatic interface implying no defaults are assumed and puppeteer-core will not download Chrome when installed.

You should use puppeteer-core if you are connecting to a remote browser or managing browsers yourself. If you are managing browsers yourself, you will need to call puppeteer.launch with an explicit executablePath (or channel if it's installed in a standard location).

When using puppeteer-core, remember to change the import:

import puppeteer from 'puppeteer-core';

Usage

Puppeteer follows the latest maintenance LTS version of Node.

Puppeteer will be familiar to people using other browser testing frameworks. You launch/connect a browser, create some pages, and then manipulate them with Puppeteer's API.

For more in-depth usage, check our guides and examples.

Example

The following example searches developer.chrome.com for blog posts with text "automate beyond recorder", click on the first result and print the full title of the blog post.

import puppeteer from 'puppeteer';

(async () => {
  // Launch the browser and open a new blank page
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Navigate the page to a URL
  await page.goto('https://developer.chrome.com/');

  // Set screen size
  await page.setViewport({width: 1080, height: 1024});

  // Type into search box
  await page.type('.devsite-search-field', 'automate beyond recorder');

  // Wait and click on first result
  const searchResultSelector = '.devsite-result-item-link';
  await page.waitForSelector(searchResultSelector);
  await page.click(searchResultSelector);

  // Locate the full title with a unique string
  const textSelector = await page.waitForSelector(
    'text/Customize and automate'
  );
  const fullTitle = await textSelector?.evaluate(el => el.textContent);

  // Print the full title
  console.log('The title of this blog post is "%s".', fullTitle);

  await browser.close();
})();

Default runtime settings

1. Uses Headless mode

By default Puppeteer launches Chrome in the Headless mode.

const browser = await puppeteer.launch();
// Equivalent to
const browser = await puppeteer.launch({headless: true});

Before v22, Puppeteer launched the old Headless mode by default. The old headless mode is now known as chrome-headless-shell and ships as a separate binary. chrome-headless-shell does not match the behavior of the regular Chrome completely but it is currently more performant for automation tasks where the complete Chrome feature set is not needed. If the performance is more important for your use case, switch to chrome-headless-shell as following:

const browser = await puppeteer.launch({headless: 'shell'});

To launch a "headful" version of Chrome, set the headless to false option when launching a browser:

const browser = await puppeteer.launch({headless: false});

2. Runs a bundled version of Chrome

By default, Puppeteer downloads and uses a specific version of Chrome so its API is guaranteed to work out of the box. To use Puppeteer with a different version of Chrome or Chromium, pass in the executable's path when creating a Browser instance:

const browser = await puppeteer.launch({executablePath: '/path/to/Chrome'});

You can also use Puppeteer with Firefox. See status of cross-browser support for more information.

See this article for a description of the differences between Chromium and Chrome. This article describes some differences for Linux users.

3. Creates a fresh user profile

Puppeteer creates its own browser user profile which it cleans up on every run.

Using Docker

See our Docker guide.

Using Chrome Extensions

See our Chrome extensions guide.

Resources

Contributing

Check out our contributing guide to get an overview of Puppeteer development.

FAQ

Our FAQ has migrated to our site.