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?

1
  • I ran your code. It is working as expected. May 7, 2015 at 4:19

5 Answers 5

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.

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()
1
  • 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, 2018 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' } } });

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")

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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