Java 使用 Selenium ChromeDriver 设置 Chrome 的语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18645205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:48:11  来源:igfitidea点击:

Set Chrome's language using Selenium ChromeDriver

javagoogle-chromeseleniumwebdriverselenium-webdriver

提问by elcharrua

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

我下载了 ChromeDriver,默认浏览器语言是英文,我需要把它改成西班牙文,我一直无法。

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

采纳答案by Yi Zeng

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

您可以通过添加 Chrome 的命令行开关“--lang”来实现。

Basically, all you need is starting ChromeDriverwith an ChromeOptionargument --lang=es, see API for details.

基本上,所有你需要开始ChromeDriverChromeOption说法--lang=es,见API的详细信息。

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

以下是 C# 代码的工作示例,用于说明如何使用 Selenium 以西班牙语启动 Chrome。

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.

Java 代码应该几乎相同(未经测试)。请记住,此处的语言环境采用语言 [-country] 形式,其中语言是 ISO-639 中的 2 个字母代码。

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
}

回答by Paul Yardley

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:

我在 Chrome 使用美国日期格式 (mm/dd/yyyy) 而不是 GB dd/mm/yyyy 格式时遇到了问题(即使我在 Chrome 中设置了这些)。使用:

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

resolved this.

解决了这个问题。

回答by Cornelius Riemenschneider

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.

对我来说,--lang 不起作用。它似乎设置了第一个打开的选项卡的语言,所有其他 chrome 进程都以 --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);

回答by omikron

For me --langalso 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 LANGenvironment variable. Tested on Linux.

对我来说--lang也没有用。我想使用特定语言(en-US 而不是 en-GB)执行 Facebook 登录测试,我发现某些页面(如 Facebook)根据系统环境变量设置界面LANG......所以如果上述答案不起作用,尝试更改LANG环境变量。在 Linux 上测试。

回答by Anuj Jain

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);

回答by AlexanderTheTester

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

截至目前(2020 年 1 月 - Chrome 版本 79.0.3945.130)接受的答案中的 C# 不起作用。

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

我目前能找到的在 C# 中工作的最简单的方法:

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("intl.accept_languages", language);
WebDriver driver = new ChromeDriver(chromeOptions);

回答by Nsy

For people using Selenium with ruby:

对于使用 Selenium 和 ruby​​ 的人:

I made it work this way:

我让它这样工作:

prefs_hash = {
       'credentials_enable_service' => false,
       'profile' => {
           'password_manager_enabled' => false,
       },
       'intl.accept_languages' => 'fr-FR', // <- here
    }

// [...]

browser = Watir::Browser.new :chrome, :prefs => prefs_hash, switches: switches, desired_capabilities: caps