Selenium 2 chrome驱动程序首选项java相当于RubyBindings [英] Selenium 2 chrome driver preferences java equivalent to RubyBindings

查看:93
本文介绍了Selenium 2 chrome驱动程序首选项java相当于RubyBindings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



然而,我发现解决方案在红宝石威盛RubyBindings,并想知道是否有一个Java等价线我可以用于此。



红宝石代码如下:

  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 =>个人档案

在搜索时,我发现chrome没有我可以像FirefoxProfile类一样使用的分析器,所以我开始使用DesireCapabilities类。经过对这个问题的进一步调查后,我发现我可以设置开关和首选项威盛capabilities.setCapabilitiy并结束以下内容:

  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功能= DesiredCapabilities.chrome();
capabilities.setCapability(chrome.prefs,prefs);
dr =新的ChromeDriver(功能);

然而,我无法得到这个工作,默认的下载目录从未改变到指定的目录一旦开始。我不确定是否存在问题,我是如何设置此功能的,或者问题在于其他地方。



最后,我最终使用了这里提出的解决方案:

http ://dkage.wordpress.com/2012/03/10/mid-air-trick-make-selenium-download-files/

但我想知道是否可以做到这一点更干净,但直接设置偏好,而不是使用UI



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



更新:

在将Selenium 2更新到2.24.1版本(以及windows chrome 22)后,出乎意料的是,地图按预期工作,现在唯一的问题是他们不推荐使用构造函数ChromeDriver(DesiredCapabilities功能),而是建议我使用ChromeOptions类,这是我无法为上述情节工作的o。



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

解决方案

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

  {
download :{
prompt_for_download:false,
default_directory:/ path / to / dir
}
}

尝试构建您的Java prefs对象并查看它是否有效。字符串vs布尔值false也可能是一个问题。


I've been looking for a way to set the driver preferences for chrome driver using java for the past two days with no luck.

I have however found a solution in ruby VIA RubyBindings and would like to know if there is a java equivalent line I can use for this.

The ruby code is the following:

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

While searching I found that chrome does not have a profiler I could use like the FirefoxProfile class, so I started using the DesireCapabilities class instead. After further investigation into this problem I found that I could set the "switches" and "prefs" VIA capabilities.setCapabilitiy and ended up with the following:

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

However I was not able to get this working, the default download directory was never changed to the specified directory once started. I am unsure if there is a problem with how I am trying to set this capability or if the problem lies elsewhere.

In the end I eventually used the solution proposed here:
http://dkage.wordpress.com/2012/03/10/mid-air-trick-make-selenium-download-files/

but I would like to know if it is possible to do this more cleanly but just setting the preferences directly instead of using the UI

Any help is appreciated, Thanks!

Update:
Surprisingly after updating Selenium 2 to version 2.24.1 (and to windows chrome 22), the code above with the Maps work as expected, the only problem now is that they deprecated the the use of the constructor ChromeDriver(DesiredCapabilities capabilities), and instead recommend I use the ChromeOptions class, which I cannot get working for the above scenario.

Below is the wiki page explaining the use of both ChromeOptions and DesiredCapabilities: http://code.google.com/p/chromedriver/wiki/CapabilitiesAndSwitches

解决方案

The Ruby bindings actually expands that to:

{
   "download": {
      "prompt_for_download": false,
      "default_directory": "/path/to/dir"
    }
}

Try building your Java prefs object like that and see if it works. The string vs boolean false could also be an issue.

这篇关于Selenium 2 chrome驱动程序首选项java相当于RubyBindings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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