7

Environment: Mac OS X 10.8.3, Ruby 2.0.0p0, selenium-webdriver 2.32.1, ChromeDriver 26.0.1383.0.

I want to change default browser language. I am testing if the site detects the browser language correctly and displays the pages in the language.

I was able to set Firefox language to German:

require "selenium-webdriver"

profile = Selenium::WebDriver::Firefox::Profile.new 
profile["intl.accept_languages"] = "de"

caps = Selenium::WebDriver::Remote::Capabilities.firefox(firefox_profile: profile) 
caps.platform = "Linux" 
caps.version = 20

driver = Selenium::WebDriver.for( 
:remote, 
url: "http://USERNAME:[email protected]:80/wd/hub", 
desired_capabilities: caps)

driver.navigate.to "http://sandbox.translatewiki.net/"

I want to do the same using Chrome (and other browsers, if possible).

I have tried several things trying to open the page in German in Chrome, but every time the page is displayed in English, instead of in German.

require "selenium-webdriver"

profile = Selenium::WebDriver::Chrome::Profile.new 
profile["intl.accept_languages"] = "de"

caps = Selenium::WebDriver::Remote::Capabilities.chrome(firefox_profile: profile) 
caps.platform = "Linux" 
caps.version = ""

driver = Selenium::WebDriver.for( 
:remote, 
url: "http://USERNAME:[email protected]:80/wd/hub", 
desired_capabilities: caps)

driver.navigate.to "http://sandbox.translatewiki.net/"

If I change firefox_profile: profile to profile: profile or chrome_profile: profile, the page opens in English (instead of in German) every time.

As far as I can see in the API docs, only :firefox_profile is supported.

I was able to do it on a local machine, but not when using Sauce Labs.

7
  • So, if you pass :firefox_profile to Chrome caps, it works or not?
    – p0deje
    Apr 25, 2013 at 15:19
  • @p0deje: It does not work. What ever I do, Chrome always opens the page in English, instead in German. Apr 25, 2013 at 15:20
  • 1
    Are you sure that if you set your default language in your browser your page loads correctly?
    – fotanus
    May 2, 2013 at 14:24
  • Wrench -> Options -> Advanced -> Change Fonts and language settings -> Language -> Google Chrome language (select your language in the drop down menu) and accept
    – fotanus
    May 2, 2013 at 14:25
  • 1
    I got that, the question is if it works without Selenium. If you change the language in chrome and nothing it still not working, you will never make selenium work either, because it is basically this what it does.
    – fotanus
    May 2, 2013 at 15:04

4 Answers 4

3
+500

This should work:

require "selenium-webdriver"

profile = Selenium::WebDriver::Chrome::Profile.new 
profile["intl.accept_languages"] = "de"

caps = Selenium::WebDriver::Remote::Capabilities.chrome(
  platform: "Linux", 
  version: "", 
  'chrome.profile' => profile.as_json['zip']
)

Selenium::WebDriver.for(:remote, 
  url: "http://[email protected]:80/wd/hub", 
  desired_capabilities: caps
)
5
  • I knew Jari is the only one who can answer this question!
    – p0deje
    May 7, 2013 at 11:03
  • The best spent 500 reputation ever. :) May 7, 2013 at 11:43
  • Has this been verified to work? It seems so close to my proposed solution that it would surprise me if one worked and the other didnt.
    – Old Pro
    May 9, 2013 at 5:43
  • would anyone know how to enable the flash plugin using the profile?
    – Richlewis
    Apr 24, 2017 at 7:58
  • The 'chrome.profile' key didn't work for me, but setting up as this comment mentions is fine: bugs.chromium.org/p/chromedriver/issues/detail?id=338#c18 May 16, 2017 at 12:44
1

Wow, the documentation for SauceLabs + Chrome + Selenium + Ruby is very inconsistent and sometimes contradictory. Unfortunately I do not have a SauceLabs account to test so all I can do is give you suggestions.

This documentation says it is a known issue that ChromeDriver does not support a custom profile. This post shows how to set a custom profile for Chrome. Go figure.

Setting a profile or a default language for that matter is NOT part of the standard WebDriver wire protocol so you may be out of luck.

One workaround would be to set your browser to use a proxy and in the proxy add/replace the Accept-Language header in the proxy.

Still, looking through the Selenium Ruby code, it looks like that post might be on to something, so give this a try:

profile = Selenium::WebDriver::Chrome::Profile.new
profile["intl.accept_languages"] = "de"

caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps['chromeOptions'] = { 'profile'    => profile.as_json['zip'] }
driver = Selenium::WebDriver.for( 
:remote, 
url: "http://USERNAME:[email protected]:80/wd/hub", 
desired_capabilities: caps)

driver.navigate.to "http://sandbox.translatewiki.net/"

EDIT: It seems like the --lang- switch does not do what you want, so ignore the following. I'm leaving it here for posterity.

This might work (forget about the profile, use command line switches):

caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps['chrome.switches'] = ['--lang-de']
3
0

I am seeing the German Translation on my local machine using:

profile = Selenium::WebDriver::Chrome::Profile.new
profile["intl.accept_languages"] = "de"
@driver = Selenium::WebDriver.for :chrome, :profile => profile  
@target = 'http://sandbox.translatewiki.net/'

osx: 10.7.5

ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.4.2]

1
  • Rich, as the last sentence in my question says, I am able to do it on a local machine (please notice link to my blog post), but not when using Sauce Labs. Apr 25, 2013 at 15:39
0

For now you can just use this method

  def launch_browser options={}

    language = options.fetch(:language, "en_US")
    url = options.fetch(:url, "www.google.com")

    prefs = {
        :intl => {
            :accept_languages => language
        }
    }
    browser = Watir::Browser.new :chrome, :prefs => prefs

    browser.goto url
  end

and then you just have to call

    launch_browser :language => "de"

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.