Set Chrome's language using Selenium ChromeDriver
Asked Answered
T

13

36

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")
}
Tails answered 5/9, 2013 at 20:20 Comment(0)
P
38

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
}
Pulcheria answered 5/9, 2013 at 21:11 Comment(1)
Has anybody the solution with Appium to test chrome on a mobile device ?Slush
F
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);
Ferrotype answered 20/9, 2016 at 13:49 Comment(2)
this worked for me using reliply.org/tools/requestheaders.php to verify.Hodgson
For me it also works without the DesiredCapabilities (first and last line).Otterburn
L
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.

Luz answered 16/12, 2015 at 13:30 Comment(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.0Repairman
S
7

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);
Sanies answered 20/1, 2020 at 14:59 Comment(0)
I
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);
Itself answered 30/5, 2018 at 10:11 Comment(0)
J
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.

Justiciable answered 16/2, 2017 at 10:18 Comment(1)
en-US and en-GB do not work for me. Who can help me?Choriamb
I
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

Identification answered 25/11, 2022 at 17:32 Comment(3)
"should work" or "does work"?Engelhardt
@Engelhardt does work.Identification
Don't work for meEngelhardt
G
3

From Chrome v117

You need to set intl.selected_languages & intl.accept_languages,

ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
prefs.put("intl.accept_languages", language); //language: "es"
prefs.put("intl.selected_languages", language);
options.setExperimentalOption("prefs", prefs);
Gilletta answered 5/10, 2023 at 16:0 Comment(0)
C
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)

Clovis answered 23/3, 2021 at 19:27 Comment(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.Hoseahoseia
O
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)
Odyssey answered 13/9, 2022 at 16:41 Comment(0)
S
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

Scrubber answered 17/3, 2020 at 16:39 Comment(0)
E
0

With latest version of Chrome 122.0.6261.129. It took almost more than 3 hours to find exact solution. Please use below:

options.addArguments("--accept-lang=de-DE");
Exuberate answered 15/3, 2024 at 18:44 Comment(1)
Learn how to use markdown. Also, I suggest that you take the tour and visit the help center in order to learn how this website works.Personable
S
0

In JavaScript this worked for me. inside the wdio.conf.js file I configurate the capabilities like this. And then in the terminal run the npm install.

capabilities: [{
        maxInstances: 7,
        browserName: 'chrome',
        browserVersion: '126.0.6478.127',
        'goog:chromeOptions' : {
            prefs: {
                download: {
                    'default_directory': downloadDir,
                    'directory_upgrade': false,
                    'promt_for_download': false
                },
                'intl.accept_languages': 'en,en_US'
            }
        }
    }],
Strychnic answered 28/6, 2024 at 16:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.