How to take a full page screenshot with Ruby and Selenium

Install Selenium Webdrivers for Ruby

gem install webdrivers

Take full page screenshot and save to file

require 'webdrivers'

options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')

driver = Selenium::WebDriver.for :chrome, options: options

driver.get "https://www.bbc.co.uk/news/business-63709754"

width  = driver.execute_script("return Math.max(document.body.scrollWidth,document.body.offsetWidth,document.documentElement.clientWidth,document.documentElement.scrollWidth,document.documentElement.offsetWidth);")
height = driver.execute_script("return Math.max(document.body.scrollHeight,document.body.offsetHeight,document.documentElement.clientHeight,document.documentElement.scrollHeight,document.documentElement.offsetHeight);")

driver.manage.window.resize_to(width, height)

driver.manage.window.maximize

sleep 5

driver.save_screenshot("./full-page.png")

Find out how to change the size of the viewport with Ruby and Selenium.

We created Urlbox to make converting HTML to images easy

Learn more about our website screenshot API.