# Puppeteer API v0.9.0 ##### Table of Contents - [Puppeteer](#puppeteer) * [class: Puppeteer](#class-puppeteer) + [puppeteer.connect(options)](#puppeteerconnectoptions) + [puppeteer.launch([options])](#puppeteerlaunchoptions) * [class: Browser](#class-browser) + [browser.close()](#browserclose) + [browser.newPage()](#browsernewpage) + [browser.version()](#browserversion) + [browser.wsEndpoint()](#browserwsendpoint) * [class: Page](#class-page) + [event: 'console'](#event-console) + [event: 'dialog'](#event-dialog) + [event: 'error'](#event-error) + [event: 'frameattached'](#event-frameattached) + [event: 'framedetached'](#event-framedetached) + [event: 'framenavigated'](#event-framenavigated) + [event: 'load'](#event-load) + [event: 'pageerror'](#event-pageerror) + [event: 'request'](#event-request) + [event: 'requestfailed'](#event-requestfailed) + [event: 'requestfinished'](#event-requestfinished) + [event: 'response'](#event-response) + [page.$(selector)](#pageselector) + [page.addScriptTag(url)](#pageaddscripttagurl) + [page.click(selector[, options])](#pageclickselector-options) + [page.close()](#pageclose) + [page.emulate(options)](#pageemulateoptions) + [page.evaluate(pageFunction, ...args)](#pageevaluatepagefunction-args) + [page.evaluateOnNewDocument(pageFunction, ...args)](#pageevaluateonnewdocumentpagefunction-args) + [page.exposeFunction(name, puppeteerFunction)](#pageexposefunctionname-puppeteerfunction) + [page.focus(selector)](#pagefocusselector) + [page.frames()](#pageframes) + [page.goBack(options)](#pagegobackoptions) + [page.goForward(options)](#pagegoforwardoptions) + [page.goto(url, options)](#pagegotourl-options) + [page.hover(selector)](#pagehoverselector) + [page.injectFile(filePath)](#pageinjectfilefilepath) + [page.keyboard](#pagekeyboard) + [page.mainFrame()](#pagemainframe) + [page.mouse](#pagemouse) + [page.pdf(options)](#pagepdfoptions) + [page.plainText()](#pageplaintext) + [page.press(key[, options])](#pagepresskey-options) + [page.reload(options)](#pagereloadoptions) + [page.screenshot([options])](#pagescreenshotoptions) + [page.setContent(html)](#pagesetcontenthtml) + [page.setExtraHTTPHeaders(headers)](#pagesetextrahttpheadersheaders) + [page.setRequestInterceptionEnabled(value)](#pagesetrequestinterceptionenabledvalue) + [page.setUserAgent(userAgent)](#pagesetuseragentuseragent) + [page.setViewport(viewport)](#pagesetviewportviewport) + [page.title()](#pagetitle) + [page.tracing](#pagetracing) + [page.type(text, options)](#pagetypetext-options) + [page.uploadFile(selector, ...filePaths)](#pageuploadfileselector-filepaths) + [page.url()](#pageurl) + [page.viewport()](#pageviewport) + [page.waitFor(selectorOrFunctionOrTimeout[, options])](#pagewaitforselectororfunctionortimeout-options) + [page.waitForFunction(pageFunction[, options, ...args])](#pagewaitforfunctionpagefunction-options-args) + [page.waitForNavigation(options)](#pagewaitfornavigationoptions) + [page.waitForSelector(selector[, options])](#pagewaitforselectorselector-options) * [class: Keyboard](#class-keyboard) + [keyboard.down(key[, options])](#keyboarddownkey-options) + [keyboard.sendCharacter(char)](#keyboardsendcharacterchar) + [keyboard.up(key)](#keyboardupkey) * [class: Mouse](#class-mouse) + [mouse.click(x, y, [options])](#mouseclickx-y-options) + [mouse.down([options])](#mousedownoptions) + [mouse.move(x, y)](#mousemovex-y) + [mouse.up([options])](#mouseupoptions) * [class: Tracing](#class-tracing) + [tracing.start(options)](#tracingstartoptions) + [tracing.stop()](#tracingstop) * [class: Dialog](#class-dialog) + [dialog.accept([promptText])](#dialogacceptprompttext) + [dialog.defaultValue()](#dialogdefaultvalue) + [dialog.dismiss()](#dialogdismiss) + [dialog.message()](#dialogmessage) + [dialog.type](#dialogtype) * [class: Frame](#class-frame) + [frame.$(selector)](#frameselector) + [frame.addScriptTag(url)](#frameaddscripttagurl) + [frame.childFrames()](#framechildframes) + [frame.evaluate(pageFunction, ...args)](#frameevaluatepagefunction-args) + [frame.injectFile(filePath)](#frameinjectfilefilepath) + [frame.isDetached()](#frameisdetached) + [frame.name()](#framename) + [frame.parentFrame()](#frameparentframe) + [frame.title()](#frametitle) + [frame.uploadFile(selector, ...filePaths)](#frameuploadfileselector-filepaths) + [frame.url()](#frameurl) + [frame.waitFor(selectorOrFunctionOrTimeout[, options])](#framewaitforselectororfunctionortimeout-options) + [frame.waitForFunction(pageFunction[, options, ...args])](#framewaitforfunctionpagefunction-options-args) + [frame.waitForSelector(selector[, options])](#framewaitforselectorselector-options) * [class: ElementHandle](#class-elementhandle) + [elementHandle.click([options])](#elementhandleclickoptions) + [elementHandle.dispose()](#elementhandledispose) + [elementHandle.evaluate(pageFunction, ...args)](#elementhandleevaluatepagefunction-args) + [elementHandle.hover()](#elementhandlehover) * [class: Request](#class-request) + [request.abort()](#requestabort) + [request.continue([overrides])](#requestcontinueoverrides) + [request.headers](#requestheaders) + [request.method](#requestmethod) + [request.postData](#requestpostdata) + [request.response()](#requestresponse) + [request.url](#requesturl) * [class: Response](#class-response) + [response.buffer()](#responsebuffer) + [response.headers](#responseheaders) + [response.json()](#responsejson) + [response.ok](#responseok) + [response.request()](#responserequest) + [response.status](#responsestatus) + [response.text()](#responsetext) + [response.url](#responseurl) ## Puppeteer Puppeteer is a Node library which provides a high-level API to control Chromium over the DevTools Protocol. ### class: Puppeteer Puppeteer module provides a method to launch a Chromium instance. The following is a typical example of using a Puppeteer to drive automation: ```js const puppeteer = require('puppeteer'); puppeteer.launch().then(async browser => { let page = await browser.newPage(); await page.goto('https://www.google.com'); // other actions... browser.close(); }); ``` #### puppeteer.connect(options) - `options` <[Object]> - `browserWSEndpoint` <[string]> a [browser websocket endpoint](#browserwsendpoint) to connect to. - `ignoreHTTPSErrors` <[boolean]> Whether to ignore HTTPS errors during navigation. Defaults to `false`. - returns: <[Promise]<[Browser]>> This methods attaches Puppeteer to an existing Chromium instance. #### puppeteer.launch([options]) - `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields: - `ignoreHTTPSErrors` <[boolean]> Whether to ignore HTTPS errors during navigation. Defaults to `false`. - `headless` <[boolean]> Whether to run Chromium in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). Defaults to `true`. - `executablePath` <[string]> Path to a Chromium executable to run instead of bundled Chromium. If `executablePath` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). - `slowMo` <[number]> Slows down Puppeteer operations by the specified amount of milliseconds. Useful so that you can see what is going on. - `args` <[Array]<[string]>> Additional arguments to pass to the Chromium instance. List of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/). - `dumpio` <[boolean]> Whether to pipe browser process stdout and stderr into `process.stdout` and `process.stderr`. Defaults to `false`. - returns: <[Promise]<[Browser]>> Promise which resolves to browser instance. The method launches a browser instance with given arguments. The browser will be closed when the parent node.js process is closed. > **Note** Puppeteer works best with the version of Chromium it is bundled with. There is no guarantee it will work with any other version. Use `executablePath` option with extreme caution. If Google Chrome (rather than Chromium) is preferred, a [Chrome Canary](https://www.google.com/chrome/browser/canary.html) or [Dev Channel](https://www.chromium.org/getting-involved/dev-channel) build is suggested. ### class: Browser A Browser is created when Puppeteer connects to a Chromium instance, either through [`puppeteer.launch`](#puppeteerlaunchoptions) or [`puppeteer.connect`](#puppeteerconnectoptions). An example of using a [Browser] to create a [Page]: ```js const puppeteer = require('puppeteer'); puppeteer.launch().then(async browser => { let page = await browser.newPage(); await page.goto('https://example.com'); browser.close(); }); ``` #### browser.close() Closes browser with all the pages (if any were opened). The browser object itself is considered to be disposed and could not be used anymore. #### browser.newPage() - returns: <[Promise]<[Page]>> Promise which resolves to a new [Page] object. #### browser.version() - returns: <[Promise]<[string]>> For headless Chromium, this is similar to `HeadlessChrome/61.0.3153.0`. For non-headless, this is similar to `Chrome/61.0.3153.0`. > **NOTE** the format of browser.version() might change with future releases of Chromium. #### browser.wsEndpoint() - returns: <[string]> Browser websocket url. Browser websocket endpoint which could be used as an argument to [puppeteer.connect](#puppeteerconnect). ### class: Page Page provides methods to interact with a single tab in Chromium. One [Browser] instance might have multiple [Page] instances. This example creates a page, navigates it to a URL, and then saves a screenshot: ```js const puppeteer = require('puppeteer'); puppeteer.launch().then(async browser => { let page = await browser.newPage(); await page.goto('https://example.com'); await page.screenshot({path: 'screenshot.png'}); browser.close(); }); ``` #### event: 'console' - <[string]> Emitted when JavaScript within the page calls one of console API methods, e.g. `console.log` or `console.dir`. Also emitted if the page throws an error or a warning. The arguments passed into `console.log` appear as arguments on the event handler. An example of handling `console` event: ```js page.on('console', (...args) => { for (let i =0; i < args.length; ++i) console.log(`${i}: ${args[i]}`); }); page.evaluate(() => console.log(5, 'hello', {foo: 'bar'})); ``` #### event: 'dialog' - <[Dialog]> Emitted when a JavaScript dialog appears, such as `alert`, `prompt`, `confirm` or `beforeunload`. Puppeteer can respond to the dialog via [Dialog]'s [accept](#dialogacceptprompttext) or [dismiss](#dialogdismiss) methods. #### event: 'error' - <[Error]> Emitted when the page crashes. > **Note** `error` event has a special meaning in Node, see [error events](https://nodejs.org/api/events.html#events_error_events) for details. #### event: 'frameattached' - <[Frame]> Emitted when a frame is attached. #### event: 'framedetached' - <[Frame]> Emitted when a frame is detached. #### event: 'framenavigated' - <[Frame]> Emitted when a frame is navigated to a new url. #### event: 'load' Emitted when the JavaScriot [`load`](https://developer.mozilla.org/en-US/docs/Web/Events/load) event is dispatched. #### event: 'pageerror' - <[string]> The exception message Emitted when an uncaught exception happens within the page. #### event: 'request' - <[Request]> Emitted when a page issues a request. The [request] object is read-only. In order to intercept and mutate requests, see `page.setRequestInterceptionEnabled`. #### event: 'requestfailed' - <[Request]> Emitted when a request fails, for example by timing out. #### event: 'requestfinished' - <[Request]> Emitted when a request finishes successfully. #### event: 'response' - <[Response]> Emitted when a [response] is received. #### page.$(selector) - `selector` <[string]> Selector to query page for - returns: <[Promise]<[ElementHandle]>> The method runs `document.querySelector` within the page. If no element matches the selector, the return value resolve to `null`. Shortcut for [page.mainFrame().$(selector)](#frameselector). #### page.addScriptTag(url) - `url` <[string]> Url of the `