How to Take Screenshots Using Ruby

Post by
Andrew Pierno
How to Take Screenshots Using Ruby

When you want to monitor user behavior, do automated testing, or you’re busy with a web scraping project, you’ll need to take screenshots. Previously, we showed you how to take screenshots using Python.

Today, we’re talking to all the Ruby developers out there! Here’s how to take screenshots using Ruby or a much simpler alternative.

Read on!

Taking Screenshots Using Ruby & Selenium

When you do some research online, you'll find that one of the quickest options to take screenshots using Ruby is via Selenium.

It has a range of tools that automate web browsers, so you can emulate user interactions and perform typical user actions (including taking screenshots). 

Your first step in using Selenium for taking screenshots is installing the WebDriver.

(P.S. If Ruby isn’t your framework of choice, check if it’s installed on your system. Use the following command in your terminal or command prompt:

ruby -v

If Ruby is installed, this command will return your version number. If it doesn’t return anything or gives an error, you’ll need to install Ruby first. Choose the correct installation method by reviewing the official Ruby documentation.)

Once you’ve confirmed Ruby’s installed, install the WebDriver with the following command:

gem install selenium-webdriver 

With everything up & running, let’s get to writing!

First, import the WebDriver into your script:

 require ‘selenium-webdriver’

 You’ll then set the width and height of your screenshot by defining the variables:

 width = 1920
height = 1080

 You’ll then start the session with the browser by opening it. In this example, we’ll use Google Chrome with this code:

 driver = Selenium::WebDriver.for :chrome

 The code opens the browser, and to take the screenshot, you’ll need to navigate to the website you want a screenshot of: ‘’

 With the website open, you’ll then execute a script to resize the browser window based on the variables you defined earlier.

 driver.execute_script %Q{
  window.resizeTo(#{width}, #{height});

 Once done, you can take the screenshot and provide a filename under which you want it to be saved:


Once done, close the browser:


 An Easier Option to Take Screenshots in Ruby

 Now, using Selenium to take screenshots can be a painful process. (We usually get a headache.)

For one, you’ll have to write a script for every website you want a screenshot of. Also, it doesn’t allow you to take full-page screenshots easily.

Luckily, there is a simpler option! 

Introducing: ScreenshotAPI for Ruby Screenshots

Taking programmatic, timed, and (almost) unlimited screenshots is as easy as punching in the ScreenshotAPI key.

Once you’ve snapped the perfect shot, export it in different file formats and resolutions quickly and easily. If you’re a designer who wants to capture the brilliance of the lazy load or parallax scrolling, we’ve got you! You can even inject custom CSS.

Ruby, we’re sorry - but there’s a better way to take screenshots! Grab your ScreenshotAPI key for free.