1# Puppeteer 2 3<!-- [START badges] --> 4 5[![Build status](https://github.com/puppeteer/puppeteer/workflows/run-checks/badge.svg)](https://github.com/puppeteer/puppeteer/actions?query=workflow%3Arun-checks) [![npm puppeteer package](https://img.shields.io/npm/v/puppeteer.svg)](https://npmjs.org/package/puppeteer) 6 7<!-- [END badges] --> 8 9<img src="https://user-images.githubusercontent.com/10379601/29446482-04f7036a-841f-11e7-9872-91d1fc2ea683.png" height="200" align="right"> 10 11###### [API](https://github.com/puppeteer/puppeteer/blob/v10.0.0/docs/api.md) | [FAQ](#faq) | [Contributing](https://github.com/puppeteer/puppeteer/blob/main/CONTRIBUTING.md) | [Troubleshooting](https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md) 12 13> Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the [DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/). Puppeteer runs [headless](https://developers.google.com/web/updates/2017/04/headless-chrome) by default, but can be configured to run full (non-headless) Chrome or Chromium. 14 15<!-- [START usecases] --> 16 17###### What can I do? 18 19Most things that you can do manually in the browser can be done using Puppeteer! Here are a few examples to get you started: 20 21- Generate screenshots and PDFs of pages. 22- Crawl a SPA (Single-Page Application) and generate pre-rendered content (i.e. "SSR" (Server-Side Rendering)). 23- Automate form submission, UI testing, keyboard input, etc. 24- Create an up-to-date, automated testing environment. Run your tests directly in the latest version of Chrome using the latest JavaScript and browser features. 25- Capture a [timeline trace](https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference) of your site to help diagnose performance issues. 26- Test Chrome Extensions. 27<!-- [END usecases] --> 28 29Give it a spin: https://try-puppeteer.appspot.com/ 30 31<!-- [START getstarted] --> 32 33## Getting Started 34 35### Installation 36 37To use Puppeteer in your project, run: 38 39```bash 40npm i puppeteer 41# or "yarn add puppeteer" 42``` 43 44Note: When you install Puppeteer, it downloads a recent version of Chromium (~170MB Mac, ~282MB Linux, ~280MB Win) that is guaranteed to work with the API. To skip the download, or to download a different browser, see [Environment variables](https://github.com/puppeteer/puppeteer/blob/v10.0.0/docs/api.md#environment-variables). 45 46### puppeteer-core 47 48Since version 1.7.0 we publish the [`puppeteer-core`](https://www.npmjs.com/package/puppeteer-core) package, 49a version of Puppeteer that doesn't download any browser by default. 50 51```bash 52npm i puppeteer-core 53# or "yarn add puppeteer-core" 54``` 55 56`puppeteer-core` is intended to be a lightweight version of Puppeteer for launching an existing browser installation or for connecting to a remote one. Be sure that the version of puppeteer-core you install is compatible with the 57browser you intend to connect to. 58 59See [puppeteer vs puppeteer-core](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#puppeteer-vs-puppeteer-core). 60 61### Usage 62 63Puppeteer follows the latest [maintenance LTS](https://github.com/nodejs/Release#release-schedule) version of Node. 64 65Note: Prior to v1.18.1, Puppeteer required at least Node v6.4.0. Versions from v1.18.1 to v2.1.0 rely on 66Node 8.9.0+. Starting from v3.0.0 Puppeteer starts to rely on Node 10.18.1+. All examples below use async/await which is only supported in Node v7.6.0 or greater. 67 68Puppeteer will be familiar to people using other browser testing frameworks. You create an instance 69of `Browser`, open pages, and then manipulate them with [Puppeteer's API](https://github.com/puppeteer/puppeteer/blob/v10.0.0/docs/api.md#). 70 71**Example** - navigating to https://example.com and saving a screenshot as _example.png_: 72 73Save file as **example.js** 74 75```js 76const puppeteer = require('puppeteer'); 77 78(async () => { 79 const browser = await puppeteer.launch(); 80 const page = await browser.newPage(); 81 await page.goto('https://example.com'); 82 await page.screenshot({ path: 'example.png' }); 83 84 await browser.close(); 85})(); 86``` 87 88Execute script on the command line 89 90```bash 91node example.js 92``` 93 94Puppeteer sets an initial page size to 800×600px, which defines the screenshot size. The page size can be customized with [`Page.setViewport()`](https://github.com/puppeteer/puppeteer/blob/v10.0.0/docs/api.md#pagesetviewportviewport). 95 96**Example** - create a PDF. 97 98Save file as **hn.js** 99 100```js 101const puppeteer = require('puppeteer'); 102 103(async () => { 104 const browser = await puppeteer.launch(); 105 const page = await browser.newPage(); 106 await page.goto('https://news.ycombinator.com', { 107 waitUntil: 'networkidle2', 108 }); 109 await page.pdf({ path: 'hn.pdf', format: 'a4' }); 110 111 await browser.close(); 112})(); 113``` 114 115Execute script on the command line 116 117```bash 118node hn.js 119``` 120 121See [`Page.pdf()`](https://github.com/puppeteer/puppeteer/blob/v10.0.0/docs/api.md#pagepdfoptions) for more information about creating pdfs. 122 123**Example** - evaluate script in the context of the page 124 125Save file as **get-dimensions.js** 126 127```js 128const puppeteer = require('puppeteer'); 129 130(async () => { 131 const browser = await puppeteer.launch(); 132 const page = await browser.newPage(); 133 await page.goto('https://example.com'); 134 135 // Get the "viewport" of the page, as reported by the page. 136 const dimensions = await page.evaluate(() => { 137 return { 138 width: document.documentElement.clientWidth, 139 height: document.documentElement.clientHeight, 140 deviceScaleFactor: window.devicePixelRatio, 141 }; 142 }); 143 144 console.log('Dimensions:', dimensions); 145 146 await browser.close(); 147})(); 148``` 149 150Execute script on the command line 151 152```bash 153node get-dimensions.js 154``` 155 156See [`Page.evaluate()`](https://github.com/puppeteer/puppeteer/blob/v10.0.0/docs/api.md#pageevaluatepagefunction-args) for more information on `evaluate` and related methods like `evaluateOnNewDocument` and `exposeFunction`. 157 158<!-- [END getstarted] --> 159 160<!-- [START runtimesettings] --> 161 162## Default runtime settings 163 164**1. Uses Headless mode** 165 166Puppeteer launches Chromium in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). To launch a full version of Chromium, set the [`headless` option](https://github.com/puppeteer/puppeteer/blob/v10.0.0/docs/api.md#puppeteerlaunchoptions) when launching a browser: 167 168```js 169const browser = await puppeteer.launch({ headless: false }); // default is true 170``` 171 172**2. Runs a bundled version of Chromium** 173 174By default, Puppeteer downloads and uses a specific version of Chromium so its API 175is guaranteed to work out of the box. To use Puppeteer with a different version of Chrome or Chromium, 176pass in the executable's path when creating a `Browser` instance: 177 178```js 179const browser = await puppeteer.launch({ executablePath: '/path/to/Chrome' }); 180``` 181 182You can also use Puppeteer with Firefox Nightly (experimental support). See [`Puppeteer.launch()`](https://github.com/puppeteer/puppeteer/blob/v10.0.0/docs/api.md#puppeteerlaunchoptions) for more information. 183 184See [`this article`](https://www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/) for a description of the differences between Chromium and Chrome. [`This article`](https://chromium.googlesource.com/chromium/src/+/master/docs/chromium_browser_vs_google_chrome.md) describes some differences for Linux users. 185 186**3. Creates a fresh user profile** 187 188Puppeteer creates its own browser user profile which it **cleans up on every run**. 189 190<!-- [END runtimesettings] --> 191 192## Resources 193 194- [API Documentation](https://github.com/puppeteer/puppeteer/blob/v10.0.0/docs/api.md) 195- [Examples](https://github.com/puppeteer/puppeteer/tree/main/examples/) 196- [Community list of Puppeteer resources](https://github.com/transitive-bullshit/awesome-puppeteer) 197 198<!-- [START debugging] --> 199 200## Debugging tips 201 2021. Turn off headless mode - sometimes it's useful to see what the browser is 203 displaying. Instead of launching in headless mode, launch a full version of 204 the browser using `headless: false`: 205 206 ```js 207 const browser = await puppeteer.launch({ headless: false }); 208 ``` 209 2102. Slow it down - the `slowMo` option slows down Puppeteer operations by the 211 specified amount of milliseconds. It's another way to help see what's going on. 212 213 ```js 214 const browser = await puppeteer.launch({ 215 headless: false, 216 slowMo: 250, // slow down by 250ms 217 }); 218 ``` 219 2203. Capture console output - You can listen for the `console` event. 221 This is also handy when debugging code in `page.evaluate()`: 222 223 ```js 224 page.on('console', (msg) => console.log('PAGE LOG:', msg.text())); 225 226 await page.evaluate(() => console.log(`url is ${location.href}`)); 227 ``` 228 2294. Use debugger in application code browser 230 231 There are two execution context: node.js that is running test code, and the browser 232 running application code being tested. This lets you debug code in the 233 application code browser; ie code inside `evaluate()`. 234 235 - Use `{devtools: true}` when launching Puppeteer: 236 237 ```js 238 const browser = await puppeteer.launch({ devtools: true }); 239 ``` 240 241 - Change default test timeout: 242 243 jest: `jest.setTimeout(100000);` 244 245 jasmine: `jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000;` 246 247 mocha: `this.timeout(100000);` (don't forget to change test to use [function and not '=>'](https://stackoverflow.com/a/23492442)) 248 249 - Add an evaluate statement with `debugger` inside / add `debugger` to an existing evaluate statement: 250 251 ```js 252 await page.evaluate(() => { 253 debugger; 254 }); 255 ``` 256 257 The test will now stop executing in the above evaluate statement, and chromium will stop in debug mode. 258 2595. Use debugger in node.js 260 261 This will let you debug test code. For example, you can step over `await page.click()` in the node.js script and see the click happen in the application code browser. 262 263 Note that you won't be able to run `await page.click()` in 264 DevTools console due to this [Chromium bug](https://bugs.chromium.org/p/chromium/issues/detail?id=833928). So if 265 you want to try something out, you have to add it to your test file. 266 267 - Add `debugger;` to your test, eg: 268 269 ```js 270 debugger; 271 await page.click('a[target=_blank]'); 272 ``` 273 274 - Set `headless` to `false` 275 - Run `node --inspect-brk`, eg `node --inspect-brk node_modules/.bin/jest tests` 276 - In Chrome open `chrome://inspect/#devices` and click `inspect` 277 - In the newly opened test browser, type `F8` to resume test execution 278 - Now your `debugger` will be hit and you can debug in the test browser 279 2806. Enable verbose logging - internal DevTools protocol traffic 281 will be logged via the [`debug`](https://github.com/visionmedia/debug) module under the `puppeteer` namespace. 282 283 # Basic verbose logging 284 env DEBUG="puppeteer:*" node script.js 285 286 # Protocol traffic can be rather noisy. This example filters out all Network domain messages 287 env DEBUG="puppeteer:*" env DEBUG_COLORS=true node script.js 2>&1 | grep -v '"Network' 288 2897. Debug your Puppeteer (node) code easily, using [ndb](https://github.com/GoogleChromeLabs/ndb) 290 291- `npm install -g ndb` (or even better, use [npx](https://github.com/zkat/npx)!) 292 293- add a `debugger` to your Puppeteer (node) code 294 295- add `ndb` (or `npx ndb`) before your test command. For example: 296 297 `ndb jest` or `ndb mocha` (or `npx ndb jest` / `npx ndb mocha`) 298 299- debug your test inside chromium like a boss! 300 301<!-- [END debugging] --> 302 303<!-- [START typescript] --> 304 305## Usage with TypeScript 306 307We have recently completed a migration to move the Puppeteer source code from JavaScript to TypeScript and as of version 7.0.1 we ship our own built-in type definitions. 308 309If you are on a version older than 7, we recommend installing the Puppeteer type definitions from the [DefinitelyTyped](https://definitelytyped.org/) repository: 310 311```bash 312npm install --save-dev @types/puppeteer 313``` 314 315The types that you'll see appearing in the Puppeteer source code are based off the great work of those who have contributed to the `@types/puppeteer` package. We really appreciate the hard work those people put in to providing high quality TypeScript definitions for Puppeteer's users. 316 317<!-- [END typescript] --> 318 319## Contributing to Puppeteer 320 321Check out [contributing guide](https://github.com/puppeteer/puppeteer/blob/main/CONTRIBUTING.md) to get an overview of Puppeteer development. 322 323<!-- [START faq] --> 324 325# FAQ 326 327#### Q: Who maintains Puppeteer? 328 329The Chrome DevTools team maintains the library, but we'd love your help and expertise on the project! 330See [Contributing](https://github.com/puppeteer/puppeteer/blob/main/CONTRIBUTING.md). 331 332#### Q: What is the status of cross-browser support? 333 334Official Firefox support is currently experimental. The ongoing collaboration with Mozilla aims to support common end-to-end testing use cases, for which developers expect cross-browser coverage. The Puppeteer team needs input from users to stabilize Firefox support and to bring missing APIs to our attention. 335 336From Puppeteer v2.1.0 onwards you can specify [`puppeteer.launch({product: 'firefox'})`](https://github.com/puppeteer/puppeteer/blob/v10.0.0/docs/api.md#puppeteerlaunchoptions) to run your Puppeteer scripts in Firefox Nightly, without any additional custom patches. While [an older experiment](https://www.npmjs.com/package/puppeteer-firefox) required a patched version of Firefox, [the current approach](https://wiki.mozilla.org/Remote) works with “stock” Firefox. 337 338We will continue to collaborate with other browser vendors to bring Puppeteer support to browsers such as Safari. 339This effort includes exploration of a standard for executing cross-browser commands (instead of relying on the non-standard DevTools Protocol used by Chrome). 340 341#### Q: What are Puppeteer’s goals and principles? 342 343The goals of the project are: 344 345- Provide a slim, canonical library that highlights the capabilities of the [DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/). 346- Provide a reference implementation for similar testing libraries. Eventually, these other frameworks could adopt Puppeteer as their foundational layer. 347- Grow the adoption of headless/automated browser testing. 348- Help dogfood new DevTools Protocol features...and catch bugs! 349- Learn more about the pain points of automated browser testing and help fill those gaps. 350 351We adapt [Chromium principles](https://www.chromium.org/developers/core-principles) to help us drive product decisions: 352 353- **Speed**: Puppeteer has almost zero performance overhead over an automated page. 354- **Security**: Puppeteer operates off-process with respect to Chromium, making it safe to automate potentially malicious pages. 355- **Stability**: Puppeteer should not be flaky and should not leak memory. 356- **Simplicity**: Puppeteer provides a high-level API that’s easy to use, understand, and debug. 357 358#### Q: Is Puppeteer replacing Selenium/WebDriver? 359 360**No**. Both projects are valuable for very different reasons: 361 362- Selenium/WebDriver focuses on cross-browser automation; its value proposition is a single standard API that works across all major browsers. 363- Puppeteer focuses on Chromium; its value proposition is richer functionality and higher reliability. 364 365That said, you **can** use Puppeteer to run tests against Chromium, e.g. using the community-driven [jest-puppeteer](https://github.com/smooth-code/jest-puppeteer). While this probably shouldn’t be your only testing solution, it does have a few good points compared to WebDriver: 366 367- Puppeteer requires zero setup and comes bundled with the Chromium version it works best with, making it [very easy to start with](https://github.com/puppeteer/puppeteer/#getting-started). At the end of the day, it’s better to have a few tests running chromium-only, than no tests at all. 368- Puppeteer has event-driven architecture, which removes a lot of potential flakiness. There’s no need for evil “sleep(1000)” calls in puppeteer scripts. 369- Puppeteer runs headless by default, which makes it fast to run. Puppeteer v1.5.0 also exposes browser contexts, making it possible to efficiently parallelize test execution. 370- Puppeteer shines when it comes to debugging: flip the “headless” bit to false, add “slowMo”, and you’ll see what the browser is doing. You can even open Chrome DevTools to inspect the test environment. 371 372#### Q: Why doesn’t Puppeteer v.XXX work with Chromium v.YYY? 373 374We see Puppeteer as an **indivisible entity** with Chromium. Each version of Puppeteer bundles a specific version of Chromium – **the only** version it is guaranteed to work with. 375 376This is not an artificial constraint: A lot of work on Puppeteer is actually taking place in the Chromium repository. Here’s a typical story: 377 378- A Puppeteer bug is reported: https://github.com/puppeteer/puppeteer/issues/2709 379- It turned out this is an issue with the DevTools protocol, so we’re fixing it in Chromium: https://chromium-review.googlesource.com/c/chromium/src/+/1102154 380- Once the upstream fix is landed, we roll updated Chromium into Puppeteer: https://github.com/puppeteer/puppeteer/pull/2769 381 382However, oftentimes it is desirable to use Puppeteer with the official Google Chrome rather than Chromium. For this to work, you should install a `puppeteer-core` version that corresponds to the Chrome version. 383 384For example, in order to drive Chrome 71 with puppeteer-core, use `chrome-71` npm tag: 385 386```bash 387npm install puppeteer-core@chrome-71 388``` 389 390#### Q: Which Chromium version does Puppeteer use? 391 392Look for the `chromium` entry in [revisions.ts](https://github.com/puppeteer/puppeteer/blob/main/src/revisions.ts). To find the corresponding Chromium commit and version number, search for the revision prefixed by an `r` in [OmahaProxy](https://omahaproxy.appspot.com/)'s "Find Releases" section. 393 394#### Q: Which Firefox version does Puppeteer use? 395 396Since Firefox support is experimental, Puppeteer downloads the latest [Firefox Nightly](https://wiki.mozilla.org/Nightly) when the `PUPPETEER_PRODUCT` environment variable is set to `firefox`. That's also why the value of `firefox` in [revisions.ts](https://github.com/puppeteer/puppeteer/blob/main/src/revisions.ts) is `latest` -- Puppeteer isn't tied to a particular Firefox version. 397 398To fetch Firefox Nightly as part of Puppeteer installation: 399 400```bash 401PUPPETEER_PRODUCT=firefox npm i puppeteer 402# or "yarn add puppeteer" 403``` 404 405#### Q: What’s considered a “Navigation”? 406 407From Puppeteer’s standpoint, **“navigation” is anything that changes a page’s URL**. 408Aside from regular navigation where the browser hits the network to fetch a new document from the web server, this includes [anchor navigations](https://www.w3.org/TR/html5/single-page.html#scroll-to-fragid) and [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) usage. 409 410With this definition of “navigation,” **Puppeteer works seamlessly with single-page applications.** 411 412#### Q: What’s the difference between a “trusted" and "untrusted" input event? 413 414In browsers, input events could be divided into two big groups: trusted vs. untrusted. 415 416- **Trusted events**: events generated by users interacting with the page, e.g. using a mouse or keyboard. 417- **Untrusted event**: events generated by Web APIs, e.g. `document.createEvent` or `element.click()` methods. 418 419Websites can distinguish between these two groups: 420 421- using an [`Event.isTrusted`](https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted) event flag 422- sniffing for accompanying events. For example, every trusted `'click'` event is preceded by `'mousedown'` and `'mouseup'` events. 423 424For automation purposes it’s important to generate trusted events. **All input events generated with Puppeteer are trusted and fire proper accompanying events.** If, for some reason, one needs an untrusted event, it’s always possible to hop into a page context with `page.evaluate` and generate a fake event: 425 426```js 427await page.evaluate(() => { 428 document.querySelector('button[type=submit]').click(); 429}); 430``` 431 432#### Q: What features does Puppeteer not support? 433 434You may find that Puppeteer does not behave as expected when controlling pages that incorporate audio and video. (For example, [video playback/screenshots is likely to fail](https://github.com/puppeteer/puppeteer/issues/291).) There are two reasons for this: 435 436- Puppeteer is bundled with Chromium — not Chrome — and so by default, it inherits all of [Chromium's media-related limitations](https://www.chromium.org/audio-video). This means that Puppeteer does not support licensed formats such as AAC or H.264. (However, it is possible to force Puppeteer to use a separately-installed version Chrome instead of Chromium via the [`executablePath` option to `puppeteer.launch`](https://github.com/puppeteer/puppeteer/blob/v10.0.0/docs/api.md#puppeteerlaunchoptions). You should only use this configuration if you need an official release of Chrome that supports these media formats.) 437- Since Puppeteer (in all configurations) controls a desktop version of Chromium/Chrome, features that are only supported by the mobile version of Chrome are not supported. This means that Puppeteer [does not support HTTP Live Streaming (HLS)](https://caniuse.com/#feat=http-live-streaming). 438 439#### Q: I am having trouble installing / running Puppeteer in my test environment. Where should I look for help? 440 441We have a [troubleshooting](https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md) guide for various operating systems that lists the required dependencies. 442 443#### Q: How do I try/test a prerelease version of Puppeteer? 444 445You can check out this repo or install the latest prerelease from npm: 446 447```bash 448npm i --save puppeteer@next 449``` 450 451Please note that prerelease may be unstable and contain bugs. 452 453#### Q: I have more questions! Where do I ask? 454 455There are many ways to get help on Puppeteer: 456 457- [bugtracker](https://github.com/puppeteer/puppeteer/issues) 458- [Stack Overflow](https://stackoverflow.com/questions/tagged/puppeteer) 459 460Make sure to search these channels before posting your question. 461 462<!-- [END faq] --> 463