9

I'm using Chrome Web Driver 2.10 chromedriver_win32.zip with Selenium WebDriver 2.31.2.

With verbose logging enabled it seems the DesiredCapabilities (https://sites.google.com/a/chromium.org/chromedriver/capabilities) are passed just fine,

[1.174][FINE]:      Initializing session with capabilities {

   "browserName": "chrome",

   "chrome.switches": [  ],

   "chromeOptions": {

      "args": [  ],

      "binary": "",

      "extensions": [  ],

      "prefs": {

         "download.default_directory": "C:\\Downloads",

         "download.directory_upgrade": "true",

         "download.extensions_to_open": "",

         "download.prompt_for_download": "false"

      }

   },

   "javascriptEnabled": true,

   "platform": "WINDOWS",

   "version": ""

}

but Chrome Web Driver is playing *.mp4 instead of downloading.

I've tried the solution at How to set Chrome preferences using Selenium Webdriver .NET binding? but it doesn't seem to work with newer Chrome Web Driver version, and it crashes if i try and use selenium-dotnet-2.31.2 with chromedriver_win_26.0.1383.0.

Anybody has a suggestion?

jww
  • 83,594
  • 69
  • 338
  • 732
Matija Grcic
  • 11,718
  • 3
  • 59
  • 85
  • Try to ensure the download directory exists. I had a mistake where the directory didn't exist and Chrome would pop up a dialog asking where to save. – Chloe Jul 05 '15 at 01:34

3 Answers3

7
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory", getClass().getResource("/data/input").toString().replace("%20", " ").replace("file:","").replaceFirst("/", ""));
options.setExperimentalOption("prefs", prefs);

options.addArguments("--test-type");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
Wictor Chaves
  • 595
  • 1
  • 9
  • 18
Prerit Jain
  • 125
  • 1
  • 5
  • "getClass().getResource("/data/input").toString().replace("%20", " ").replace("file:","").replaceFirst("/", "")" is a path to maven target/data/input – Prerit Jain Feb 13 '15 at 11:11
3

I got this working by below code:

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
String downloadFilepath = "/path/to/download";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOptions("prefs", chromePrefs);
options.addArguments("--test-type");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
Suresh Raja
  • 359
  • 4
  • 21
-5

This worked greatly for me:

DesiredCapabilities cap = DesiredCapabilities.firefox();

FirefoxProfile fprofile= new FirefoxProfile();
fprofile.setPreference("browser.download.folderList",2);  //0-desktop,1-file download folder,2-specified location
fprofile.setPreference("browser.download.manager.showWhenStarting", false); //prevent download file window
fprofile.setPreference("browser.download.dir","E:\\Downloadfilebyprogram");
fprofile.setPreference("browser.download.manager.focusWhenStarting", false);
//fprofile.setPreference("browser.helperApps.alwaysAsk.force", false);
//fprofile.setPreference("browser.download.manager.closeWhenDone", true);
//fprofile.setPreference("browser.download.manager.useWindow", false);
//fprofile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
//fprofile.setPreference("browser.helperApps.neverAsk.openFile,","application/vnd.ms-excel");
fprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/vnd.ms-excel");
fprofile.setPreference("browser.download.manager.alertOnExeOpen", false); //prevent from opening a file

cap.setCapability(FirefoxDriver.PROFILE, fprofile);
WebDriver driver=new FirefoxDriver(cap);
Wictor Chaves
  • 595
  • 1
  • 9
  • 18
Sathish
  • 23
  • 2