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

查看:3110
本文介绍了使用Selenium ChromeDriver设置Chrome的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了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")
}


推荐答案

您可以通过添加Chrome的命令行开关--lang。

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

基本上,从 ChromeDriver 开始 ChromeOption 参数 - lang = es ,详情请参阅API。

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

以下是使用Selenium以西班牙语启动Chrome的C#代码示例。

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代码应该几乎相同(未经测试)。请记住,此处的语言环境采用格式语言[-country],其中语言是来自ISO-639的双字母代码。

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
}

这篇关于使用Selenium ChromeDriver设置Chrome的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆