Watir and Headless Chrome on MacOS X


Install Chromedriver

Using Homebrew

brew install chromedriver

# Start chromedriver
chromedriver
# or
brew services start chromedriver

I initially installed Chromedriver this way and later experienced some issues when switching projects as well as having to remember to start Chromedriver every time. Since then I have found another solution and recommend to use it instead: use webdrivers gem. Just add the gem to Gemfile and it's done.

gem 'webdrivers'
gem 'watir'

Initialize Watir with headless chrome

Use headless: true option to initialize Watir with headless Chrome

require 'watir'
browser = Watir::Browser.new :chrome, headless: true
browser.goto 'google.com'

In the recent versions of Watir, switches option no longer works. Need to pass args option instead

args = ["--proxy-server=host:port", "--headless"]
browser = Watir::Browser.new :chrome, options: {args: args}