6

I am trying to set my browser locale by doing the following

options = webdriver.ChromeOptions()
options.add_argument('--lang=es')
driver = webdriver.Chrome(executable_path=driver_location, chrome_options=options)

When I browse to google the site is still displayed in english. Am I setting the "--lang" option wrong?

  • I ran your code. It is working as expected. – LittlePanda May 7 '15 at 4:19
13

Not Working

I've tried the --lang=us option, as suggested, but also had no success using the code below. My webpages were still being downloaded with my default language (that's not en).

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('lang=en')
# options.add_argument('--lang=en') <- Tried this option as well
driver = webdriver.Chrome(chrome_options=options)

Working

After some research I found this gist that finally helped me. Their solution is to use the experimental option intl.accept_languages, as follows:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
driver = webdriver.Chrome(chrome_options=options)

By doing that, pages started to be loaded with the language that I wanted.

Also, Google Chrome source code explains that this option will actually set the HTTP header Accept-Languages to the given value. What's exactly what I wanted to do.

| improve this answer | |
2

Try and convert this to the Python equivalent. Check this link:

For Firefox:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference( "intl.accept_languages", "es" ); 
WebDriver driver = new FirefoxDriver(profile);

For Chrome:

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

Python equivalent that worked for me:

from selenium import webdriver
from time import sleep
driver_location = "res/chromedriver.exe"

options = webdriver.ChromeOptions()
options.add_argument('--lang=es')
driver = webdriver.Chrome(executable_path=driver_location, chrome_options=options)

driver.get("http://google.com")
sleep(3000)
driver.quit()
| improve this answer | |
  • Thanks. Your code worked for me both for Chrome and Firefox. This is missing Python code for Firefox: firefox_profile = webdriver.FirefoxProfile() firefox_profile.set_preference('intl.accept_languages', 'es') driver = webdriver.Firefox(executable_path=driver_location, firefox_profile=firefox_profile) – Alex Che Aug 23 '18 at 19:55
1

For Perl users here is the magic string:

Selenium::Chrome

my $driver = Selenium::Chrome->new( 'extra_capabilities' => { 'chromeOptions' => { 'prefs' => { 'intl.accept_languages' => 'es' } } });

| improve this answer | |
0

The same thing happens when I switch Chrome's default language. There are two solutions I can think of - manually add the Accept-Language: es header, or go directly to Google's Spanish version of the homepage - driver.get("http://google.es")

| improve this answer | |
0

You can add hl parameter to your query like https://www.google.com/?hl=es

Reference: https://sites.google.com/site/tomihasa/google-language-codes

you can define the web interface language to be English by adding hl=en to the end of the URL and after the change the URL will look like this: http://www.google.com/search?hl=en

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.