https://sites.google.com/a/chromium.org/chromedriver/capabilities
Capabilities are options that you can use to customize and configure a ChromeDriver session. This page documents all ChromeDriver supported capabilities and how to use them.
有2种方法来指定capabilities.
1.使用ChromeOptions class.
2.如果客户端的library没有ChromeOptions class (ruby client没有),可以通过DesiredCapabilities 来直接指定capabilities.
一、用ChromeOptions class
实例化一个ChromeOptions, 之后直接传入ChromeOptions到ChromeDriver constructor.
//实例化一个ChromeOptions
ChromeOptions options = new ChromeOptions();
//安装一个插件
options.addExtensions(new File("/path/to/extension.crx"));
//传入到ChromeDriver
ChromeDriver driver = new ChromeDriver(options);
或者
可以把Options添加到一个已存在的DesiredCapabilities对象上。当你需要指定不仅是ChromeDriver 还包括其他WebDriver的capabalities,这样就比较方便了。
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// 添加 WebDriver proxy capability.
Proxy proxy = new Proxy();
proxy.setHttpProxy("myhttpproxy:3337");
capabilities.setCapability("proxy", proxy);
// 添加ChromeDriver-specific capabilities through ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
//传入到ChromeDriver
ChromeDriver driver = new ChromeDriver(capabilities);
二、直接用DesiredCapabilities
下面的代码,ChromeOptions直接使用了DesiredCapabilities,这就需要大家了解各种Capabilities的名字和值类型了。
Map<String, Object> chromeOptions = new Map<String, Object>();
chromeOptions.put("binary", "/usr/lib/chromium-browser/chromium-browser");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver = new ChromeDriver(capabilities);
三、常见的使用案例
Use custom profile (also called user data directory)
ChromeDriver会默认为每个Session创建一个新的缓存文件。
如果你需要设置特殊的preferences—-可以用“Chrome.prefs”capability 来自定义。
如果只需要使用默认的preferences—-可以用user-data-dir命令行,来告诉chrome用哪个profile
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
打开最大化Chrome
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
Using a Chrome executable in a non-standard location
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");
Set a Chrome preference
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
options.setExperimentalOption("prefs", prefs);