Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 689
MITHU Avatar asked Mar 13 '19 19:03

MITHU


People also ask

What is Selenium language bindings?

The term language binding generally refers to mapping a software library/API to another language in the manner to be used in two different developing ecosystems and/or environments with the same functionalities. The number of Selenium users has grown significantly over time.

How do I change python to Windows Selenium?

For Selenium to switch to a particular window, one needs to use the switch_to_window method. Pass the window handle ID of the target tab where the user wants to switch as an argument to that method.


2 Answers

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)
like image 164
Abhishek Dhoundiyal Avatar answered Sep 29 '22 02:09

Abhishek Dhoundiyal


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")
like image 37
C. Peck Avatar answered Sep 29 '22 03:09

C. Peck