Selenium change language browser Chrome / Firefox

19,023

Solution 1

The answer is already available in one of the very recent post:
Change language on Firefox with Selenium Python

Here is the code:

def get_webdriver(attempts=3, timeout=60, locale='en-us'):
  firefox_profile = webdriver.FirefoxProfile()
  firefox_profile.set_preference("intl.accept_languages", locale)
  firefox_profile.update_preferences()

  desired_capabilities = getattr(
      DesiredCapabilities, "FIREFOX").copy()

  hub_url = urljoin('http://hub:4444', '/wd/hub')
  driver = webdriver.Remote(
    command_executor=hub_url, desired_capabilities=desired_capabilities,
    browser_profile=firefox_profile)

  return driver

Solution 2

FIREFOX JAVA

Java code for changing the language to English:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", "en-GB");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
driver = new FirefoxDriver(options);

CHROME JAVA

Java code for changing the language to English:

ChromeOptions options = new ChromeOptions();
options.addArguments("lang=en-GB");
driver = new ChromeDriver(options);

FIREFOX PYTHON

options = Options()
profile = webdriver.FirefoxProfile()
profile.set_preference('intl.accept_languages', 'en-GB')
browser = webdriver.Firefox(options=options,firefox_profile=profile)

Solution 3

I have this java code please modify it in python

Using Firefox Browser :

FirefoxProfile profile = new FirefoxProfile();
//setting the locale french : ‘fr’
profile.setPreference(“intl.accept_languages”,”fr”);
driver = new FirefoxDriver(profile);
driver.get(“http://google.co.in);

Using Chrome Browser :

System.setProperty(“webdriver.chrome.driver”,”D:/DollarArchive/chromedriver.exe”);
ChromeOptions options = new ChromeOptions();
options.addArguments(“–lang= sl”);
ChromeDriver driver = new ChromeDriver(options);
driver.get(“http://google.co.in);

In python set something like below

For firefox

driver.set_preference(“intl.accept_languages”,”fr”)

For Chrome

options.add_argument(“–lang= sl”)

Hope it will help you :)

Solution 4

This code apply to the simplest use case with browser running on local machine.

For Firefox:

from selenium import webdriver

browser_locale = 'fr'
gecko_driver_path = 'geckodriver64.exe'

profile = webdriver.FirefoxProfile()
profile.set_preference('intl.accept_languages', browser_locale)

browser = webdriver.Firefox(executable_path=gecko_driver_path,
                            firefox_profile=profile)
browser.get('https://google.com/')

For Chrome:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

browser_locale = 'fr'
chrome_driver_path = 'chromedriver.exe'

options = Options()
options.add_argument("--lang={}".format(browser_locale))

browser = webdriver.Chrome(executable_path=chrome_driver_path,
                           chrome_options=options)
browser.get('https://google.com/')
Share:
19,023
DaschPyth
Author by

DaschPyth

Updated on June 15, 2022

Comments

  • DaschPyth
    DaschPyth almost 2 years

    I'm trying to test my website with Selenium but I don't manage to change the language of the browser. I tried with Firefox, changing the profile also but it's not working.

    That's a pity because much of my content is changing regarding the language.

    Here is my Python code:

    @classmethod
    def setUpClass(cls):
        super(SeleniumTestCase, cls).setUpClass()
        options = Options()
        options.add_argument('--lang=en')
        cls.selenium = WebDriver(chrome_options=options)
    

    So normally I change the language but nothing happens...

    Just to clarify. I already checked on stackoverflow and if I post this question it's really because I tried most of the solutions I saw.