使用Java中的Selenium WebDriver下载zip文件 - 下载Popup即将推出

时间:2014-11-20 18:37:08

标签: java selenium automation

使用以下代码我从网站下载文件,我使用的是带有版本2.43的Selenium jar的Firefox 32.0.3。

    FirefoxProfile  firefoxProfile = new FirefoxProfile();
    firefoxProfile.setPreference("browser.download.folderList",2);
    firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
    firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
    firefoxProfile.setPreference("browser.download.dir","C:\\RDM_Files");
    firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip;application/octet-stream;application/x-zip;application/x-zip-compressed");
    firefoxProfile.setPreference("plugin.disable_full_page_plugin_for_types", "application/zip");

    WebDriver driver = new FirefoxDriver(firefoxProfile);

我检查了我正在下载的文件的MIME类型是application / zip。每次我尝试下载文件时,我都会看到一个要求打开文件或保存文件的窗口。

我搜索了Stackoverflow.com&发现与处理.pdf文件相关的帖子,但不是zip文件。请帮忙

2 个答案:

答案 0 :(得分:2)

我认为你正在寻找像这样的东西

//common to all the cases
FirefoxProfile prof = new FirefoxProfile();

//Case:1 - Use this case to set download this code to your browser's default location
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

//Case:2 - Download file to Desktop
//prof.setPreference("browser.download.folderList", 0);
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

//Case:3 - Download to custom folder path. Replace d:\\selenium with your Download Location 
prof.setPreference("browser.download.dir","D:\\selenium\\");
prof.setPreference("browser.download.folderList", 2);
prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

//This will work for all cases mentioned above
WebDriver driver = new FirefoxDriver(prof);
driver.get("http://docs.seleniumhq.org/download/");
driver.findElement(By.xpath("//tr[1]/td[4]/a[text()='Download']")).click();

答案 1 :(得分:0)

禁用弹出系统非网页下载/保存对话框。

FirefoxProfile prof = new FirefoxProfile();

ffprofile.setPreference("browser.download.panel.shown", false);
ffprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");

//ffprofile.setPreference("browser.download.folderList", 1);  // Default to /home/user/Downloads in Linux.
ffprofile.setPreference("browser.download.folderList", 2); 
ffprofile.setPreference("browser.download.dir", "/tmp");