Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Selenium Ruby’s save_screenshot method after navigating to the page. By default, it saves a PNG of the current browser viewport—not the entire page.

Take a viewport screenshot with Selenium in Ruby

Install the selenium-webdriver gem and make sure the browser you select is available to Selenium. This example opens a page, saves a PNG in the current directory, and quits the browser even if an error occurs:

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome

begin
  driver.get 'https://example.com/'
  driver.save_screenshot('./screenshot.png')
ensure
  driver.quit
end

Replace the URL with the page you want to capture. The output path is relative to the directory where you run the Ruby program; use an absolute path if you want to choose a specific location. The parent directory must already exist and be writable. Use a .png filename: the Ruby API writes PNG data and warns when the extension does not match.

What part of the page does the screenshot include?

Viewport capture

driver.save_screenshot('./screenshot.png') captures the active browser context’s visible viewport. It does not automatically include content below the fold.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall

Full-page capture

The current Ruby API reference documents full_page: true when full-page screenshots are supported:

driver.save_screenshot('./full-page.png', full_page: true)

This is conditional, not a cross-browser guarantee. If the installed Selenium, browser, or driver combination does not support it, the call can raise an unsupported-operation error. Check the API documentation matching your installed gem and verify support in your actual setup before depending on this option. The API reference also labels the screenshot module private and warns that it may change or be removed.

One element

To capture a specific element rather than the whole viewport, find it and call save_screenshot on the element:

element = driver.find_element(css: '#main-content')
element.save_screenshot('./element.png')

Use a .png extension here too; the documented saving method writes PNG data even though Selenium’s example uses a different extension.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshoot common problems

  • The screenshot file is missing: Confirm the browser reached the screenshot line, that the path is writable, and that its parent directory exists. Relative paths are resolved from the process’s working directory.
  • The file cannot be opened as an image: Save with a .png extension. The method writes PNG data; a mismatched extension can mislead image viewers or downstream tools.
  • The screenshot is cropped to what was visible: That is the default viewport behavior. Try full_page: true only if your installed browser and Selenium combination supports it; otherwise capture the relevant element or use another workflow designed for full-page capture.
  • Full-page capture raises an unsupported-operation error: Remove the option to take a viewport screenshot, or confirm support for your specific Selenium gem, browser, and driver versions before retrying.
  • The element screenshot fails: Verify the selector identifies an element on the loaded page before calling element.save_screenshot.

Or skip the browser setup

ScreenshotNeo takes a screenshot with one GET request, without requiring you to start and manage a browser in your Ruby script. It removes cookie banners, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and the response identifies the page verdict and billing status in headers. It also provides an MCP server for AI agents, including Claude, Cursor, and other MCP clients.

Example cURL request (see the ScreenshotNeo API documentation):

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com/ -o shot.webp

The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Learn about ScreenshotNeo or sign up for 1,000 free screenshots a month with no card.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Does Selenium save screenshots as PNG in Ruby?

Yes. The documented Ruby screenshot method writes PNG data, so use a filename ending in .png.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Can I take a screenshot of an element instead of the page?

Yes. Find the element with find_element, then call save_screenshot on it.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.