Custom User-Agent using Selenium-Webdriver Remote and Ruby bindings/Capybara

08 Nov 2011 | By Matt Robbins | Tags testing webdriver selenium cucumber ruby

As far as I understand it the Selenium-Webdriver project made an architectural decision not to provide an API for maniplulating headers when running ‘in-browser’ tests. This implied that the only way of doing this would be to use an intermediate proxy such as browsermob-proxy, which for most people might be fine. However within my work infrastructure introducing such a proxy was not feasible.

So when I needed to do some ‘in-browser’ testing using different User-Agents I was delighted to find there is in fact a way around this…using Chrome Switches.

You set these using the Webdriver capabilities when starting your tests, below is an example using the Ruby bindings directly and as an alternative using Capybara. This example is using the API for the client bindings directly (i.e. opening browser on your local machine)

#starting the browser from the client bindings, setting a custom User-Agent
driver = Selenium::WebDriver.for :chrome, :switches => ['--user-agent=Mozilla/5.0 (PLAYSTATION 3; 3.55)']
#example using Capybara (
opts[:switches] = ['--user-agent=Mozilla/5.0 (PLAYSTATION 3; 3.55)'],opts)

The api is a little different when using the remote Webdriver…

#starting the browser from the remote bindings, setting a custom User-Agent
remote_opts['chrome.switches'] = ['--user-agent=Mozilla/5.0 (PLAYSTATION 3; 3.55)']
remote_caps =
driver = WebDriver.for(:remote, :desired_capabilities => remote_caps)
#example using Capybara (
remote_opts['chrome.switches'] = ['--user-agent=Mozilla/5.0 (PLAYSTATION 3; 3.55)']
remote_caps =
opts[:desired_capabilities] = caps,opts)

Note: if your using Capybara 1 or greater I think the only difference would be the namespacing of the Selenium Driver…so something like:,opts)

blog comments powered by Disqus