WebDriver: how to specify preferred languages ​​for Chrome

I am trying to get my remote chrome driver to request pages in German, not English. Following the chrome documentation> and the list of chrome preferences , I tried to install it as follows:

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 see that it reaches the chronograph 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 he still requests English pages, and this can also be seen by opening the content settings in the settings. What am I doing wrong?

+6
source share
2 answers

(Edit) In short:

  • intl.accept_languages is the preference key for managing language requests for a page.
  • Set preferences using the (newer and preferred) ChromeOptions (otherwise, it will not work if any ChromeOptions settings are set by you or by language links, see Problems 104 and 95 ).

    Support for ChromeOptions settings is not yet fully implemented. So unfortunately you have to use the dirty workaround from my comment on release 95

    Alternative it can be creating a user profile with the required language settings and using ChromeOption to set the option (command line) for using this profile, as indicated in the wiki page with chrome channels .

+3
source

Pyhon examples

Note. I am testing it with the accepted language "en, en_US", but I do not understand why it will not work with de_DE if the language 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') 

There are 2 options for a shard:

Splinter Interface 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') 

Shard 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') 
0
source

Source: https://habr.com/ru/post/919478/


All Articles