8

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?

c089
  • 4,951
  • 2
  • 25
  • 37

2 Answers2

7

(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.

zpea
  • 1,072
  • 6
  • 23
  • This makes a lot of sense, especially as it's the same key firefox uses. But it does not work, either :/ – c089 Jul 08 '12 at 08:48
  • Yeah, now I can confirm it for calls from python as well. I added some text about what I think goes wrong (including a bug report) and what might be an alternative solution / workaround until it gets fixed. It's probably not the answer you hoped for. But I hope it helps somewhat, nevertheless. – zpea Jul 09 '12 at 21:35
  • Thanks for your effort and filing the bug :) – c089 Jul 10 '12 at 06:29
  • In my new shortened down answer there is also a link to a java example for working around the ChromeOptions vs. "classic" Capabilities issue and the lacking preferences support in ChromeOptions. Let me know if it solves your problem! :) – zpea Jul 19 '12 at 11:35
  • The refleciton hack is no longer neccessary as experimentalOptions is now exposed: https://gist.github.com/c089/a5cbb834f9b54004de9b – c089 Jun 12 '13 at 09:33
  • 1
    Please note that this preference will be ignored in headless mode! see also https://bugs.chromium.org/p/chromedriver/issues/detail?id=3992 – Sephiroth Jan 10 '22 at 10:29
0

Pyhon examples

Note: I test it with "en,en_US" accepted language, but I don't see why it wouldn't work with de_DE as long as locale is available on the system.

This work with selenium

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from splinter.driver.webdriver import BaseWebDriver, WebDriverElement

options = Options()
options.add_experimental_option('prefs', {'intl.accept_languages': 'de_DE'})

browser = BaseWebDriver()
browser.driver = Chrome(chrome_options=options)

browser.visit('http://example.com')

With splinter there is 2 options :

Splinter API only

from splinter import Browser
from splinter.driver.webdriver.chrome import Options

options = Options()
options.add_experimental_option('prefs', {'intl.accept_languages': 'de_DE'})

browser = Browser('chrome', options=options)

browser.visit('http://example.com')

Splinter and selenium API

from splinter import Browser
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {'intl.accept_languages': 'de_DE'})

browser = Browser('chrome', options=options)

browser.visit('http://example.com')
Richard
  • 721
  • 5
  • 16
  • 2
    It's an old answer but in my case (year 2022) I had to use `de-DE` instead of `de_DE`. On http://www.reliply.org/tools/requestheaders.php I could see that `de-DE` was also expanded to `de-DE,de;q=0.9` while `de_DE` only resulted in `de_DE`. – Kelvin Dec 12 '22 at 11:57