'Trouble modifying the language option in selenium python bindings

I've created a script in python in combination with selenium to scrape different app names from google play store and they all are coming through when I execute my script. However, the result are being converted into my native language that is not English.

How can I modify the language option in selenium python bindings?

My attempt (tried to change the language option but failed):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

link = 'https://play.google.com/store'

chrome_options = Options()
chrome_options.add_argument("accept-language=en-US")

with webdriver.Chrome(options=chrome_options) as driver:
    driver.get(link)
    for item in wait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.details a.title'))):
        print(item.text)

The output I'm having are in my native language which is not english.



Solution 1:[1]

Not Working:

I've tried the --lang, but it didn't worked for me:

chrome_options.add_argument("--lang=en")
            OR
chrome_options.add_argument("--lang=en-US")

Working Solution:

After some research I found that to solve this, we have to use the experimental option intl.accept_languages:

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

Note: To use above, your website should need to support the same.

There is one more way to achieve the same by translating your native language page to english:

Try using below code:

prefs = {
  "translate_whitelists": {"your native language":"en"},
  "translate":{"enabled":"True"}
}
options.add_experimental_option("prefs", prefs)

Solution 2:[2]

I think your syntax for setting the language is off. Instead of

chrome_options.add_argument("accept-language=en-US")

Try

chrome_options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})

Second Round

Looks like english isn't available in 'intl.accept_languages' yet. But after a little more searching I found the following might work--give it a try?

chrome_options.add_argument("--lang=en-US")

Third round

Try using --lang=en-GB as Fenio suggests:

chrome_options.add_argument("--lang=en-GB")

Solution 3:[3]

Try

chrome_options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})

Solution 4:[4]

All available switches for chromium (most should apply to chrome as well) can be found at: https://peter.sh/experiments/chromium-command-line-switches/#lang

I have not found --accept-language as one of them, but --lang=locale is one of them.

To be able to use it, the profile that will open the chrome session need to support it (being part of the supported language).

By default it won't. Only your native language will be part of it. To add it, you can create new profiles

The below code will use one language per profile. Even if --lang=locale should work if your profile has it, it can be tricky as the Chrome UI implementation will use the one set by the main/first chrome window open. See @sarjoor answer for details explanation: How language work across multiple chrome windows

Now to create new profile: How to create a chrome profile

When your profile is created and you have started a session with it, go to: chrome://settings/languages

Add your desired languages to it. Move the language on top of the list.

Then we need to specify the two profile switches:

chrome_options.add_argument('--user-data-dir=profileDirectory')
chrome_options.add_argument('--profile-directory="yourProfileName))

Your profile directory and Name can be found by going to: chrome://version

On macOS it is located at:
Profile Path /Users/auto/Library/Application Support/Google/Chrome/Profile 1

profile_map = {
    'en': 'Default',
    'fr': 'Profile 1',
    'de': 'Profile 2'
}
chrome_options.add_argument('--user-data-dir=/Users/auto/Library/Application Support/Google/Chrome')
chrome_options.add_argument('--profile-directory={}'.format(profile_map['de']))

You might want to add the --lang switch but if the desired language for the profile is the default one it should work without it.

chrome_options.add_argument("--lang='de'")

with webdriver.Chrome(options=chrome_options) as driver:
    driver.get(link)
    for item in wait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.details a.title'))):
        print(item.text)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Bhavya Lodaya
Solution 3 CDeck
Solution 4 Nic Laforge