Set Chrome's language using Selenium ChromeDri

2020-02-01 00:43发布

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")
}

6条回答
beautiful°
2楼-- · 2020-02-01 00:56

As of now (Jan 2020 - Chrome Version 79.0.3945.130) The C# in the accepted answer does not work.

The simplest approach that I can find to work in C# presently:

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("intl.accept_languages", language);
WebDriver driver = new ChromeDriver(chromeOptions);
查看更多
孤傲高冷的网名
3楼-- · 2020-02-01 01:03

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
}
查看更多
来,给爷笑一个
4楼-- · 2020-02-01 01:10

I was trying the same and above listed nothing worked for me, at last I tried the below and it worked:

ChromeOptions chromeOptions = new ChromeOptions();

Map<String, Object> prefs = new HashMap<String, Object>();

prefs.put("intl.accept_languages", "ja-jp,ja");

chromeOptions.setExperimentalOption("prefs", prefs);

WebDriver driver = new ChromeDriver(chromeOptions);
查看更多
够拽才男人
5楼-- · 2020-02-01 01:13

For me --lang also didn't work. I wanted to perform Facebook Login tests with specific language (en-US instead of en-GB) and what I found is that some pages (like Facebook) set interface according to system environment variable LANG... So if above answers doesn't work, try changing LANG environment variable. Tested on Linux.

查看更多
孤傲高冷的网名
6楼-- · 2020-02-01 01:16

I had problems with Chrome using US date format (mm/dd/yyyy) instead of the GB dd/mm/yyyy format (even though I had set these in Chrome). Using:

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

resolved this.

查看更多
疯言疯语
7楼-- · 2020-02-01 01:17

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);
查看更多
登录 后发表回答