Setting a Language with Selenium Webdriver in Ruby
01.23.2017
Hello reader! This post was inspired by a project I was working on
recently, adding a 'lang' option to a Selenium-Webdriver (the ruby
gem) wrapper. I needed to instantiate
Selenium::Webdriver.for(:firefox) and (:chrome) both locally and
remotely (Sauce labs, specifically), setting a locale
for each. There's plenty of documentation on how to do this in
Java, C, and even Python, but it was hard to find the right
way to do it using the ruby gem. So here's what worked for me, where
@@sweep_options[:lang]
is just a variable that holds either
'en-us' or 'ja' (as strings).
Locally
Firefox
firefox_profile = Selenium::WebDriver::Firefox::Profile.new
firefox_profile['intl.accept_languages'] = @@sweep_options[:lang]
Selenium::WebDriver.for(:firefox,
:http_client => client,
:profile => firefox_profile)
Chrome
chrome_prefs = { :lang => @@sweep_options[:lang] }
Selenium::WebDriver.for(:chrome,
:http_client => client,
:prefs => chrome_prefs,
:desired_capabilities => caps)
Remotely
Firefox
firefox_profile = Selenium::WebDriver::Firefox::Profile.new
firefox_profile['intl.accept_languages'] = @@sweep_options[:lang]
caps = Selenium::WebDriver::Remote::Capabilities.firefox(:firefox_profile => firefox_profile)
sweep_driver = Selenium::WebDriver.for(
:remote,
:http_client => client,
:url => URL,
:desired_capabilities => caps)
Chrome
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" =>
{"args" => ["--lang=#{@@sweep_options[:lang]}"]})
sweep_driver = Selenium::WebDriver.for(
:remote,
:http_client => client,
:url => URL,
:desired_capabilities => caps)