Examples

Screenshot of a particular element with Ruby and Puppeteer

Install the Ruby Puppeteer port

gem install puppeteer-ruby

puppeteer-ruby drives a Chrome you already have installed — it does not download its own browser. Pass channel: "chrome" (as below) to use the system Chrome, or set executable_path: to point at a specific Chromium binary.

Take a screenshot of the element and save to file

require 'puppeteer-ruby'

Puppeteer.launch(channel: 'chrome') do |browser|
  page = browser.new_page
  page.goto('https://www.htmltoimage.com/demo')
  element = page.query_selector('#example-card')
  bounding_box = element.bounding_box
  page.screenshot(
    path: './element.png',
    clip: { x: bounding_box.x, y: bounding_box.y, width: bounding_box.width, height: bounding_box.height }
  )
end

Find out how to take a full-page screenshot with Ruby and Puppeteer.

That was 12 lines of code — plus a browser to run in production

Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:

curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com"

Try the Urlbox screenshot API — free trial, no browser babysitting.