简体   繁体   English

Selenium WebDriver - 在 linux 上更改浏览器的语言

[英]Selenium WebDriver - changing browser's language on linux

Opening a browser (google chrome) in a different language through selenium WebDriver works fine when running on PC, as described here .在PC上运行时,如所描述的,通过硒的webdriver不同的语言打开浏览器(Google Chrome浏览器)工作正常这里 But when trying it on linux based systems, or mac-os, it simply doesn't work, and the browser opens on it's default language.但是当在基于 linux 的系统或 mac-os 上尝试它时,它根本不起作用,并且浏览器以其默认语言打开。 I tried using different language code, such as "es_ES" or "es-ES" instead of "es", but nothing helped.我尝试使用不同的语言代码,例如“es_ES”或“es-ES”而不是“es”,但没有任何帮助。 Is it a different language code for linux, or is it a different way to manipulate the web driver and not use the "--lang" command?它是 linux 的不同语言代码,还是操作 Web 驱动程序而不使用“--lang”命令的不同方式?

Thanks.谢谢。

I haven't try it but I think you can change the setting from chrome itself as :-我还没有尝试过,但我认为您可以将 chrome 本身的设置更改为:-

settings -> Lamguages -> Add languages.设置 -> 语言 -> 添加语言。

Add your language there and give a try.在那里添加您的语言并尝试一下。 remove other language if required.如果需要,删除其他语言。

For IE refer below link :-对于 IE,请参阅以下链接:-

http://www.reliply.org/info/internet/http-accept-lang.html http://www.reliply.org/info/internet/http-accept-lang.html

I have also found a code on same link you shared.我还在您分享的同一链接上找到了一个代码。 Have you tried it?你试过吗?

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

Source :-来源:-

Set Chrome's language using Selenium ChromeDriver 使用 Selenium ChromeDriver 设置 Chrome 的语言

也许你还需要设置prefs > intl > accept_language: en-GB

"desiredCapabilities": { "browserName": "chrome", "chromeOptions": { "args": ["--lang=en-GB"], "prefs": { "intl": { "accept_languages": "en-GB" } } } }

As you can read at developer.chrome.com , there is a system-dependand way to set language for Chrome.正如您在developer.chrome.com 上看到的,有一种系统相关的方式来设置 Chrome 的语言。 On Linux, a environment variable is required.在 Linux 上,需要一个环境变量。

I created a Bash script like this:我创建了一个像这样的 Bash 脚本:

#!/bin/sh

LANGUAGE="en" "/home/plap/projects/pdf-exporter/chromedriver" $*

Then I use the path of the script in the place of the path of the real chromedriver executable.然后我使用脚本的路径代替真正的 chromedriver 可执行文件的路径。

Moreover, since I need to swich language programmatically, I made code to save scripts like that programmatically at each new language required.此外,由于我需要以编程方式切换语言,因此我编写了代码以在所需的每种新语言中以编程方式保存脚本。 Indeed the code calls:事实上,代码调用:

System.setProperty("webdriver.chrome.driver", executableFile.getAbsolutePath());

in a synchronized block together with new ChromeDriver(options) ... Yes, it's horrible!new ChromeDriver(options)一起在同步块中......是的,这太可怕了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM