Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriver: How to specify preferred languages for Chrome

I'm trying to get my remote chrome driver to request pages in German instead of English. Following the chromedriver documentation and list of chrome preferences, I tried to set it like this:

    capabilities.setCapability(ChromeOptions.CAPABILITY, getChromeOptions());
    Map<String, String> chromePrefs = new HashMap<String,String>();
    chromePrefs.put("settings.language.preferred_languages", "de-DE,de");
    capabilities.setCapability("chrome.prefs", chromePrefs);

And I can see it reaches chromedriver from the log file:

[0.453][FINE]:      Initializing session with capabilities {
   "browserName": "chrome",
   "chrome.prefs": {
      "settings.language.preferred_languages": "de-DE,de"
   },
   "chromeOptions": {
      "args": [ "--ignore-certificate-errors" ],
      "extensions": [  ]
   },
   "platform": "ANY",
   "version": null
}

But it still requests english pages and this can also be seen by opening the content settings in the preferences. What am I doing wrong?

like image 683
c089 Avatar asked Jul 02 '12 07:07

c089


1 Answers

(Edit) Long story short:

  1. intl.accept_languages is the preferences key to manipulate what languages are requested for a page.
  2. Set the capability for the preferences using the (newer and preferred) ChromeOptions mechanism (otherwise it won't work if any ChromeOptions are set by you or your language bindings, see Issues 104 & 95).

    ChromeOptions support for setting preferences is not completely implemented yet. So, unfortunately, you have to use the dirty workaround from my comment 6 to Issue 95

    An alternative might be to create a user profile with the desired language settings and use ChromeOption to set the (command line) option to use this profile, as mentioned on the chromedriver capabilities wiki page.

like image 89
zpea Avatar answered Oct 07 '22 23:10

zpea