Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 534
DaschPyth Avatar asked Oct 08 '15 12:10

DaschPyth


People also ask

How to change Chrome language in Selenium?

In Selenium, we can modify the language preferences with the help of ChromeOptions class for the Chrome browser. We shall create an object of this class and apply addArguments method on it. To modify the language to Spanish, we have to pass −−lang=es as a parameter to the addArguments method.

Can I use Selenium with Firefox?

Mozilla Firefox is one of the most widely used browsers in the world. It has enhanced features and is supported by a multitude of the latest testing tools and techniques. One such tool is Selenium.


2 Answers

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()
options.set_preference('intl.accept_languages', 'en-GB')
browser = webdriver.Firefox(options=options)
like image 109
Norayr Sargsyan Avatar answered Sep 18 '22 02:09

Norayr Sargsyan


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
like image 42
Pappu Jha Avatar answered Sep 19 '22 02:09

Pappu Jha