Examples
Screenshot of a particular size with Ruby and Selenium
Install Selenium for Ruby
gem install selenium-webdriver selenium-webdriver 4.11+ includes Selenium Manager, which downloads the right chromedriver automatically — the separate webdrivers gem is no longer needed.
Take a screenshot of the resized viewport and save to file
require 'selenium-webdriver'
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless=new')
driver = Selenium::WebDriver.for(:chrome, options: options)
driver.get('https://www.htmltoimage.com/demo')
driver.manage.window.resize_to(480, 240)
driver.save_screenshot('./size.png')
driver.quit Find out how to take a full-page screenshot with Ruby and Selenium.
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.