Selenium 2 chrome驱动程序首选项java相当于RubyBindings

Zer*_*573 6 java google-chrome webdriver selenium-webdriver

我一直在寻找一种方法来设置过去两天使用java的Chrome驱动程序的驱动程序首选项,但没有运气.

然而,我在ruby VIA RubyBindings中找到了一个解决方案,并且想知道我是否可以使用java等效行.

ruby代码如下:

profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = "/path/to/dir"

driver = Selenium::WebDriver.for :chrome, :profile => profile
Run Code Online (Sandbox Code Playgroud)

在搜索时我发现chrome没有我可以像FirefoxProfile类那样使用的探查器,所以我开始使用DesireCapabilities类.经过对这个问题的进一步调查后,我发现我可以设置"开关"和"prefs"VIA功能.设置可用性并最终得到以下结果:

Map<String, String> prefs = new Hashtable<String, String>();
prefs.put("download.prompt_for_download", "false");
prefs.put("download.default_directory", "/path/to/dir");
prefs.put("download.extensions_to_open", "pdf");

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.prefs", prefs);
dr = new ChromeDriver(capabilities);
Run Code Online (Sandbox Code Playgroud)

但是我无法使其正常工作,默认下载目录在启动后从未更改为指定目录.我不确定我是如何设置此功能的问题,或者问题是否存在于其他地方.

最后我最终使用了这里提出的解决方案:http:
//dkage.wordpress.com/2012/03/10/mid-air-trick-make-selenium-download-files/

但我想知道是否可以更干净地做到这一点,但只是直接设置首选项而不是使用UI

任何帮助表示赞赏,谢谢!

更新:
令人惊讶的是,在将Selenium 2更新到版本2.24.1(以及windows chrome 22)后,上面的代码与Maps按预期工作,现在唯一的问题是他们不赞成使用构造函数ChromeDriver(DesiredCapabilities功能),而是建议我使用ChromeOptions类,我无法在上面的场景中使用它.

以下是解释ChromeOptions和DesiredCapabilities使用情况的维基页面:http: //code.google.com/p/chromedriver/wiki/CapabilitiesAndSwitches

jar*_*rib 2

Ruby 绑定实际上将其扩展为:

{
   "download": {
      "prompt_for_download": false,
      "default_directory": "/path/to/dir"
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试像这样构建 Java prefs 对象,看看它是否有效。字符串与布尔值 false 也可能是一个问题。