Chrome Web Driver下载文件

时间:2014-05-07 23:50:33

标签: google-chrome selenium selenium-webdriver chrome-web-driver

我正在使用Chrome网络驱动程序2.10 chromedriver_win32.zipSelenium WebDriver 2.31.2

启用详细日志记录后,似乎 DesiredCapabilities https://sites.google.com/a/chromium.org/chromedriver/capabilities)传递得很好,

[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": ""

}

但Chrome网络驱动程序正在播放 *。mp4 而非下载。

我已在How to set Chrome preferences using Selenium Webdriver .NET binding?尝试了该解决方案,但它似乎不适用于较新的Chrome网络驱动程序版本,如果我尝试将selenium-dotnet-2.31.2chromedriver_win_26.0.1383.0一起使用,则会崩溃。

有人有建议吗?

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:-4)

这对我很有用:

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