Install Chrome language with Selenium ChromeDriver

I download ChromeDriver, and the default browser language is in English, I need to change it to Spanish, and I couldn’t.

public WebDriver getDriver(String locale){ System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe"); return new ChromeDriver(); } public void initializeSelenium() throws Exception{ driver = getDriver("en-us") } 
+6
source share
4 answers

You can do this by adding chrome command line switches "--lang".

Basically, all you need to do is launch ChromeDriver using the ChromeOption argument --lang=es . See the API for details.

The following is a working C # code example for running Chrome in Spanish using Selenium.

 ChromeOptions options = new ChromeOptions(); options.AddArguments("--lang=es"); ChromeDriver driver = new ChromeDriver(options); 

Java code should be approximately the same (untested). Remember that the locale here is in the language of the form [-country], where the language is the 2-letter code from ISO-639.

 public WebDriver getDriver(String locale){ System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("--lang=" + locale); return new ChromeDriver(options); } public void initializeSelenium() throws Exception{ driver = getDriver("es"); // two letters to represent the locale, or two letters + country } 
+11
source

For me, -lang does not work. It seems that it sets the language of the first open tab, all other chrome processes start with -lang = en-US.

What happened:

 DesiredCapabilities jsCapabilities = DesiredCapabilities.chrome(); ChromeOptions options = new ChromeOptions(); Map<String, Object> prefs = new HashMap<>(); prefs.put("intl.accept_languages", language); options.setExperimentalOption("prefs", prefs); jsCapabilities.setCapability(ChromeOptions.CAPABILITY, options); 
+5
source

I had problems with Chrome using the US date format (mm / dd / yyyy) instead of the GB dd / mm / yyyy format (although I installed them in Chrome). Using:

 options.addArguments("--lang=en-GB"); 

allowed it.

+2
source

For me, --lang didn't work either. I wanted to run Facebook login tests in a specific language (en-US instead of en-GB), and I found that some pages (like Facebook) set the interface according to the LANG system environment variable ... So if the answers above are not Do not work, try changing the LANG environment variable. Tested on Linux.

0
source

Source: https://habr.com/ru/post/953270/


All Articles