Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Chrome's language using Selenium ChromeDriver

I download ChromeDriver and by defaults the browser language is in English, I need to change it to Spanish, and I have been unable.

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") } 
like image 470
elcharrua Avatar asked Sep 05 '13 20:09

elcharrua


People also ask

How do I change Chrome options in Selenium?

Code Explanation: Create an object of DesiredCapabilities Chrome class and merge the Desired Capabilities class object with Chrome Options class object using merge method. Create an object of Chrome Driver class and pass the Chrome Options Selenium object as an argument.

How do I change ChromeDriver path?

Go to the terminal and type the command: sudo nano /etc/paths. Enter the password. At the bottom of the file, add the path of your ChromeDriver. Type Y to save.


2 Answers

You can do it by adding Chrome's command line switches "--lang".

Basically, all you need is starting ChromeDriver with an ChromeOption argument --lang=es, see API for details.

The following is a working example of C# code for how to start Chrome in Spanish using Selenium.

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

Java code should be pretty much the same (untested). Remember, locale here is in the form language[-country] where 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 } 
like image 167
Yi Zeng Avatar answered Sep 22 '22 13:09

Yi Zeng


For me, --lang didn't work. It seems to set the language of the first opened tab, all others chrome processes are started with --lang=en-US.

What did work is the following:

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); 
like image 32
Cornelius Riemenschneider Avatar answered Sep 22 '22 13:09

Cornelius Riemenschneider