Puppeteer notes

install puppeteer

puppeteer

npm i puppeteer

puppeteer-core

npm i puppeteer-core

use puppeteer

const puppeteer = require('puppeteer')

launch puppeteer - puppeteer.launch

const puppeteer = require('puppeteer')

;(async () => {
  const browser = await puppeteer.launch()
  // other actions...
})()

or

const puppeteer = require('puppeteer')

puppeteer.launch().then(async browser => {
  // other actions...
})

puppeteer.launch options

// slow down Puppeteer operations in milliseconds
puppeteer.launch({ slowMo: 1000 })

// disable headless mode (i.e. launch in full version of Chrome)
puppeteer.launch({ headless: false })

// launch in Chrome and disable viewport to match window
// (by default, the viewport is fixed at 800x600 no matter the window size)
puppeteer.launch({
  headless: false,
  defaultViewport: null,
})

// launch in Chrome and set window size to 800x600
puppeteer.launch({
  headless: false,
  defaultViewport: null,
  args: ['--window-size=800,600'],
})

navigate to url - page.goto

const page = await browser.newPage()
await page.goto('https://example.com')
// other actions...
await browser.close()

page.goto options

// disable navigation timeout
page.goto('https://example.com', { timeout: 0 })

// set navigation timeout (defaults to 30 seconds)
page.goto('https://example.com', { timeout: 30000 })

// consider navigation to be finished when load event is fired
page.goto('https://example.com', { waitUntil: 'load' })

// consider navigation to be finished when domcontentloaded event is fired
page.goto('https://example.com', { waitUntil: 'domcontentloaded' })

// consider navigation to be finished when there are no network connections for at least 500 ms
page.goto('https://example.com', { waitUntil: 'networkidle0' })

// consider navigation to be finished when there are 2 network connections or less for at least 500 ms
page.goto('https://example.com', { waitUntil: 'networkidle2' })

page.evaluate

page waiting

// page.waitFor
await page.waitFor(5000)

// page.waitForRequest
await page.waitForResponse(req => req.url() === 'https://example.com')

// page.waitForResponse
await page.waitForResponse(res => res.status() === 200)

// page.waitForSelector
await page.waitForSelector('#content')

Example - save a screenshot

const puppeteer = require('puppeteer')

;(async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('https://example.com')
  await page.screenshot({ path: 'example.png' })

  await browser.close()
})()

Example - create a PDF

const puppeteer = require('puppeteer')

;(async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('https://news.ycombinator.com', { waitUntil: 'networkidle2' })
  await page.pdf({ path: 'hn.pdf', format: 'A4' })

  await browser.close()
})()

Example: evaluate script in the context of the page

const puppeteer = require('puppeteer')

;(async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('https://example.com')

  // Get the "viewport" of the page, as reported by the page.
  const dimensions = await page.evaluate(() => {
    return {
      width: document.documentElement.clientWidth,
      height: document.documentElement.clientHeight,
      deviceScaleFactor: window.devicePixelRatio,
    }
  })

  console.log('Dimensions:', dimensions)

  await browser.close()
})()

Source


© 2020 | Paul Kim