puppeteer中文文档

Puppeteer API 1.2.0
预计2018.04.12发布

目录

  • 概论
  • 环境变量
  • Puppeteer类
    • puppeteer.connect(options)
    • puppeteer.createBrowserFetcher([options])
    • puppeteer.defaultArgs()
    • puppeteer.executablePath()
    • puppeteer.launch([options])
  • BrowserFetcher类
    • browserFetcher.canDownload(revision)
    • browserFetcher.download(revision[, progressCallback])
    • browserFetcher.localRevisions()
    • browserFetcher.platform()
    • browserFetcher.remove(revision)
    • browserFetcher.revisionInfo(revision)
  • Browser类
    • event: 'disconnected'
    • event: 'targetchanged'
    • event: 'targetcreated'
    • event: 'targetdestroyed'
    • browser.close()
    • browser.disconnect()
    • browser.newPage()
    • browser.pages()
    • browser.process()
    • browser.targets()
    • browser.userAgent()
    • browser.version()
    • browser.wsEndpoint()
  • Page类
    • event: 'close'
    • event: 'console'
    • event: 'dialog'
    • event: 'domcontentloaded'
    • event: 'error'
    • event: 'frameattached'
    • event: 'framedetached'
    • event: 'framenavigated'
    • event: 'load'
    • event: 'metrics'
    • event: 'pageerror'
    • event: 'request'
    • event: 'requestfailed'
    • event: 'requestfinished'
    • event: 'response'
    • page.$(selector)
    • page.$$(selector)
    • page.$$eval(selector, pageFunction[, ...args])
    • page.$eval(selector, pageFunction[, ...args])
    • page.$x(expression)
    • page.addScriptTag(options)
    • page.addStyleTag(options)
    • page.authenticate(credentials)
    • page.bringToFront()
    • page.click(selector[, options])
    • page.close()
    • page.content()
    • page.cookies(...urls)
    • page.coverage
    • page.deleteCookie(...cookies)
    • page.emulate(options)
    • page.emulateMedia(mediaType)
    • page.evaluate(pageFunction, ...args)
    • page.evaluateHandle(pageFunction, ...args)
    • page.evaluateOnNewDocument(pageFunction, ...args)
    • page.exposeFunction(name, puppeteerFunction)
    • page.focus(selector)
    • page.frames()
    • page.goBack(options)
    • page.goForward(options)
    • page.goto(url, options)
    • page.hover(selector)
    • page.keyboard
    • page.mainFrame()
    • page.metrics()
    • page.mouse
    • page.pdf(options)
    • page.queryObjects(prototypeHandle)
    • page.reload(options)
    • page.screenshot([options])
    • page.select(selector, ...values)
    • page.setCacheEnabled(enabled)
    • page.setContent(html)
    • page.setCookie(...cookies)
    • page.setDefaultNavigationTimeout(timeout)
    • page.setExtraHTTPHeaders(headers)
    • page.setJavaScriptEnabled(enabled)
    • page.setOfflineMode(enabled)
    • page.setRequestInterception(value)
    • page.setUserAgent(userAgent)
    • page.setViewport(viewport)
    • page.tap(selector)
    • page.target()
    • page.title()
    • page.touchscreen
    • page.tracing
    • page.type(selector, text[, options])
    • page.url()
    • page.viewport()
    • page.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])
    • page.waitForFunction(pageFunction[, options[, ...args]])
    • page.waitForNavigation(options)
    • page.waitForSelector(selector[, options])
    • page.waitForXPath(xpath[, options])
  • Keyboard类
    • keyboard.down(key[, options])
    • keyboard.press(key[, options])
    • keyboard.sendCharacter(char)
    • keyboard.type(text, options)
    • keyboard.up(key)
  • Mouse类
    • mouse.click(x, y, [options])
    • mouse.down([options])
    • mouse.move(x, y, [options])
    • mouse.up([options])
  • Touchscreen类
    • touchscreen.tap(x, y)
  • Tracing类
    • tracing.start(options)
    • tracing.stop()
  • Dialog类
    • dialog.accept([promptText])
    • dialog.defaultValue()
    • dialog.dismiss()
    • dialog.message()
    • dialog.type()
  • ConsoleMessage类
    • consoleMessage.args()
    • consoleMessage.text()
    • consoleMessage.type()
  • Frame类
    • frame.$(selector)
    • frame.$$(selector)
    • frame.$$eval(selector, pageFunction[, ...args])
    • frame.$eval(selector, pageFunction[, ...args])
    • frame.$x(expression)
    • frame.addScriptTag(options)
    • frame.addStyleTag(options)
    • frame.childFrames()
    • frame.click(selector[, options])
    • frame.content()
    • frame.evaluate(pageFunction, ...args)
    • frame.evaluateHandle(pageFunction, ...args)
    • frame.executionContext()
    • frame.focus(selector)
    • frame.hover(selector)
    • frame.isDetached()
    • frame.name()
    • frame.parentFrame()
    • frame.select(selector, ...values)
    • frame.setContent(html)
    • frame.tap(selector)
    • frame.title()
    • frame.type(selector, text[, options])
    • frame.url()
    • frame.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])
    • frame.waitForFunction(pageFunction[, options[, ...args]])
    • frame.waitForSelector(selector[, options])
    • frame.waitForXPath(xpath[, options])
  • ExecutionContext类
    • executionContext.evaluate(pageFunction, ...args)
    • executionContext.evaluateHandle(pageFunction, ...args)
    • executionContext.frame()
    • executionContext.queryObjects(prototypeHandle)
  • JSHandle类
    • jsHandle.asElement()
    • jsHandle.dispose()
    • jsHandle.executionContext()
    • jsHandle.getProperties()
    • jsHandle.getProperty(propertyName)
    • jsHandle.jsonValue()
  • ElementHandle类
    • elementHandle.$(selector)
    • elementHandle.$$(selector)
    • elementHandle.$x(expression)
    • elementHandle.asElement()
    • elementHandle.boundingBox()
    • elementHandle.boxModel()
    • elementHandle.click([options])
    • elementHandle.contentFrame()
    • elementHandle.dispose()
    • elementHandle.executionContext()
    • elementHandle.focus()
    • elementHandle.getProperties()
    • elementHandle.getProperty(propertyName)
    • elementHandle.hover()
    • elementHandle.jsonValue()
    • elementHandle.press(key[, options])
    • elementHandle.screenshot([options])
    • elementHandle.tap()
    • elementHandle.toString()
    • elementHandle.type(text[, options])
    • elementHandle.uploadFile(...filePaths)
  • Request类
    • request.abort([errorCode])
    • request.continue([overrides])
    • request.failure()
    • request.frame()
    • request.headers()
    • request.method()
    • request.postData()
    • request.redirectChain()
    • request.resourceType()
    • request.respond(response)
    • request.response()
    • request.url()
  • Response类
    • response.buffer()
    • response.fromCache()
    • response.fromServiceWorker()
    • response.headers()
    • response.json()
    • response.ok()
    • response.request()
    • response.securityDetails()
    • response.status()
    • response.text()
    • response.url()
  • SecurityDetails类
    • securityDetails.issuer()
    • securityDetails.protocol()
    • securityDetails.subjectName()
    • securityDetails.validFrom()
    • securityDetails.validTo()
  • Target类
    • target.createCDPSession()
    • target.page()
    • target.type()
    • target.url()
  • CDPSession类
    • cdpSession.detach()
    • cdpSession.send(method[, params])
  • Coverage类
    • coverage.startCSSCoverage(options)
    • coverage.startJSCoverage(options)
    • coverage.stopCSSCoverage()
    • coverage.stopJSCoverage()

概述

Puppeteer是一个基于开发者工具协议,为谷歌浏览器和Chromium提供高级api的Node库。
Puppeteer API对浏览器结构有分级映射。如下图所示

  • Puppeteer:通过开发者工具协议和浏览器交互
  • Browser:实例可以有很多page
  • Page:至少含有一个frame,称作主frame,可能包含其他的iframe或frame
  • Frame:含有至少一个可执行内容,默认的可执行内容是frame的JavaScript执行,一个frame可能会有与扩展相关的额外执行内容。

环境变量

Puppeteer为了增加操作,会在环境中寻找某些环境变量,如果没有找到,会使用npm的config中小写的默认变量值。

  • HTTP_PROXY, HTTPS_PROXY, NO_PROXY:定义了下载和使用Chromium的http协议的参数设置
  • PUPPETEER_SKIP_CHROMIUM_DOWNLOAD:安装过程中不捆绑下载Chromium
  • PUPPETEER_DOWNLOAD_HOST:用于覆盖下载Chromium的Url地址
  • PUPPETEER_CHROMIUM_REVISION:安装过程中,指定puppeteer使用某个版本的谷歌浏览器

Puppeteer类

Puppeteer模块是一个提供加载Chormium实例的方法,下面是使用Puppeteer实现自动化的典型实例

puppeteer.connect(options)

  • 参数(Object对象)
    • browserWSEndpoint (String)一个浏览器的websocket连接终端
    • ignoreHTTPSErrors(boolean)编译过程中是否忽略https错误,默认false
    • slowMo(number)在指定的毫秒数内放缓Puppeteer的操作,便于弄清楚发生了什么
  • 返回 (promise)
    puppeteer中connect方法跟已存在的Chromium的实例绑定

puppeteer.createBrowserFetcher([options])

  • 参数(Object)
    • host (string)要使用的下载主机 默认是https://storage.googleapis.com
    • path (string)下载文件夹的路径 默认<root>/.local-chromium <root>指puppeteer的package路径
    • platform (string)可选mac, win32, win64, linux,默认是当前的平台
  • 返回 (BrowserFetcher)
评论 ( 1 )
最新评论

原文github地址
本文会慢慢更新,水平有限,不足之处,希望各位不吝指教