Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium 2 chrome driver preferences java equivalent to RubyBindings

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

like image 250
Zero4573 Avatar asked Mar 27 '12 16:03

Zero4573


1 Answers

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.

like image 97
jarib Avatar answered Sep 22 '22 22:09

jarib