selenium snippets

How to capture screenshots with Selenium, Ruby and Firefox

Tagged ruby, selenium, screenshot  Languages ruby

Option #1 Selenium-WebDriver gem

First install the selenium-webdriver gem:

gem install selenium-webdriver

Then start using it:

require 'selenium-webdriver'
width = 1024
height = 728
driver = Selenium::WebDriver.for :firefox ''
driver.execute_script %Q{
  window.resizeTo(#{width}, #{height});


Option #2 Selenium Server and the Selenium Gem

I was using this option for quite some time, when it stopped working I started using the selenium-webdriver gem instead.

First create a separate Firefox profile:

firefox -ProfileManager -no-remote

Find the path to the profile by executing this code in the JavaScript error console in Firefox:

Components.classes[";1"].getService( Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile).path

The path looks somewhat like this on OSX:

/Users/christian/Library/Application Support/Firefox/Profiles/xxx

Now download the Selenium Server. Then start a Selenium server:

java -jar ~/Downloads/selenium-server-standalone-2.0b1.jar -firefoxProfileTemplate "/Users/christian/Library/Application Support/Firefox/Profiles/9eb0mn2d.screenshots/" -singleWindow

Next install the Selenium Ruby client:

gem install selenium-client

Take a screenshot with Ruby:

require 'rubygems'
require 'selenium'

@selenium ="localhost", 4444, "*firefox", "", 10000);
@selenium.start "/US/"
@selenium.send(:sleep, 7) # Wait for Flash and other crap to load
@selenium.capture_entire_page_screenshot('/tmp/screenshot.png', '');

Want more? Read the Selenium RC and Selenium client documentation

Set the accept-language header in Capybara / Selenium WebDriver tests

Tagged capybara, ruby, selenium  Languages ruby

To change the accept-language header that Chrome sends with each request in Capybara tests, add the ‘selenium-webdriver (3.142.6)’ preference to the driver’s options:

  opts.add_preference('intl.accept_languages', 'sv')


Capybara.register_driver :chrome do |app|
  caps = { browser: 'ALL' })
  opts =
  opts.add_preference('intl.accept_languages', 'sv')
  chrome_args = %w[--no-sandbox --disable-popup-blocking --enable-features=NetworkService,NetworkServiceInProcess --window-size=1920,1080]
  chrome_args.each { |arg| opts.add_argument(arg) }, browser: :chrome, options: opts, desired_capabilities: caps)

Tested with:

  • capybara (3.30.3)
  • selenium-webdriver (3.142.6)

How to debug and fix chromedriver and selenium errors

Tagged capybara, chromedriver, connection, refused, selenium  Languages bash

You an enable the chromedriver log to debug and fix chromedriver and Selenium errors such as “unknown error: net::ERR_CONNECTION_REFUSED”.

First start the chromedriver in the background:

nohup chromedriver --port=12345 --verbose --log-path=/tmp/chromedriver.log &

Then configure Selenium to use the chromedriver instance:, browser: :chrome, options: ..., url: 'http://localhost:12345')

Next run the tests and inspect the chromedriver log.

In my case the issue was that an unreachable URL, see the unreachableUrl property in the output below:

[1659101781.048][DEBUG]: DevTools WebSocket Event: Page.frameNavigated B30A4B9FE0908FDACE0522341267520A {
   "frame": {
      "adFrameStatus": {
         "adFrameType": "none"
      "crossOriginIsolatedContextType": "NotIsolated",
      "domainAndRegistry": "",
      "gatedAPIFeatures": [  ],
      "id": "B30A4B9FE0908FDACE0522341267520A",
      "loaderId": "595BC7FEFD2C1ECCE31157FF7972F889",
      "mimeType": "text/html",
      "secureContextType": "InsecureScheme",
      "securityOrigin": "://",
      "unreachableUrl": "http://localhost:3030/",
      "url": "chrome-error://chromewebdata/"
   "type": "Navigation"