31

I download ChromeDriver and by defaults the browser language is in English, I need to change it to Spanish, and I have been unable.

public WebDriver getDriver(String locale){   
    System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
    return new ChromeDriver();
}

public void initializeSelenium() throws Exception{
    driver = getDriver("en-us")
}
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
elcharrua
  • 1,582
  • 6
  • 30
  • 50

10 Answers10

35

You can do it by adding Chrome's command line switches "--lang".

Basically, all you need is starting ChromeDriver with an ChromeOption argument --lang=es, see API for details.

The following is a working example of C# code for how to start Chrome in Spanish using Selenium.

ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=es");
ChromeDriver driver = new ChromeDriver(options);

Java code should be pretty much the same (untested). Remember, locale here is in the form language[-country] where language is the 2 letter code from ISO-639.

public WebDriver getDriver(String locale){   
    System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--lang=" + locale);
    return new ChromeDriver(options);
}

public void initializeSelenium() throws Exception{
    driver = getDriver("es"); // two letters to represent the locale, or two letters + country
}
Philipp Kief
  • 8,172
  • 5
  • 33
  • 43
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
15

For me, --lang didn't work. It seems to set the language of the first opened tab, all others chrome processes are started with --lang=en-US.

What did work is the following:

DesiredCapabilities jsCapabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
prefs.put("intl.accept_languages", language);
options.setExperimentalOption("prefs", prefs);
jsCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
alfonx
  • 6,936
  • 2
  • 49
  • 58
  • 1
    this worked for me using http://www.reliply.org/tools/requestheaders.php to verify. –  Jun 15 '17 at 08:20
  • For me it also works without the DesiredCapabilities (first and last line). – S. Doe Mar 21 '20 at 08:56
9

I had problems with Chrome using US date format (mm/dd/yyyy) instead of the GB dd/mm/yyyy format (even though I had set these in Chrome). Using:

options.addArguments("--lang=en-GB");

resolved this.

Paul Yardley
  • 91
  • 1
  • 1
  • 1
    This worked for me whereas the accepted solution didn't (assuming for English it would be "--lang=en"). It seems it won't work just with the language two-letter code, but one must add the specific "spell" such as "GB". Edit: using the python variant on [3.141.0](https://pypi.org/project/selenium/3.141.0/) – j4nSolo Jun 09 '21 at 11:02
6

As of now (Jan 2020 - Chrome Version 79.0.3945.130) The C# in the accepted answer does not work.

The simplest approach that I can find to work in C# presently:

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("intl.accept_languages", language);
WebDriver driver = new ChromeDriver(chromeOptions);
4

I was trying the same and above listed nothing worked for me, at last I tried the below and it worked:

ChromeOptions chromeOptions = new ChromeOptions();

Map<String, Object> prefs = new HashMap<String, Object>();

prefs.put("intl.accept_languages", "ja-jp,ja");

chromeOptions.setExperimentalOption("prefs", prefs);

WebDriver driver = new ChromeDriver(chromeOptions);
DeiDei
  • 10,205
  • 6
  • 55
  • 80
Anuj Jain
  • 41
  • 1
3

For me --lang also didn't work. I wanted to perform Facebook Login tests with specific language (en-US instead of en-GB) and what I found is that some pages (like Facebook) set interface according to system environment variable LANG... So if above answers doesn't work, try changing LANG environment variable. Tested on Linux.

omikron
  • 2,745
  • 1
  • 25
  • 34
3

For latest versions below code should work.

ChromeOptions options = new ChromeOptions();
options.addArguments("--accept-lang=" + locale);
return new ChromeDriver(options);

https://peter.sh/experiments/chromium-command-line-switches/#accept-lang

1

I had the same problem. I tried to solve the problem including

chromeOptions = Options()
chromeOptions.add_argument('--lang=es')

but it didn't work (I have discovered that isn't necessary).

It works when I changed the locale:

locale -a
sudo apt-get install language-pack-es
sudo dpkg-reconfigure locales

It is es_ES.UTF-8 UTF-8 for Spanish. Finally, you have to start a new shell in order to get new env variables (LANG=C.UTF-8 to es_ES.UTF-8)

Chema
  • 19
  • 1
  • Thank you a lot! I had a multi-language website and I needed to get the content in Russian for me. I was passing the --lang argument to Chrome but the website was providing an English content version nevertheless. And only after setting up the locales, everything got working fine. – skavans Apr 27 '21 at 20:18
1
website = 'https://e-katalog.lkpp.go.id/id/search-produk?q=' + mylist[i] + 
                                 '&order=relevance&limit=12&offset=' + str(1)
chrome_options = Options()
chrome_options.add_argument("--lang=en");
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])

prefs = {
          "translate_whitelists": {'id':'en'},
          "translate":{"enabled":"True"}}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome('C:\webdrivers\chromedriver.exe',options=chrome_options)
time.sleep(10)
driver.get(website)
time.sleep(5)
0

For people using Selenium with ruby:

I made it work this way:

prefs_hash = {
       'credentials_enable_service' => false,
       'profile' => {
           'password_manager_enabled' => false,
       },
       'intl.accept_languages' => 'fr-FR', // <- here
    }

// [...]

browser = Watir::Browser.new :chrome, :prefs => prefs_hash, switches: switches, desired_capabilities: caps

Nsy
  • 71
  • 4