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

查看:79
本文介绍了使用 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".

基本上,您只需要使用 ChromeOption 参数 --lang=es,详见 API.

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

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

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 中的 2 个字母代码.

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天全站免登陆