Search code examples
pythonseleniumtesting

Selenium change language browser Chrome / Firefox


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.


Solution

  • 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