答案 0 :(得分:1)
您必须创建/修改浏览器配置文件以自动下载/保存excel文件,弹出窗口(或)可以使用Robot类来处理窗口弹出窗口。
参考How to download any file and save it to the desired location using Selenium Webdriver
答案 1 :(得分:0)
我认为你正在寻找像这样的东西
//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");
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;");
//Case:2 - Download file to Desktop
//prof.setPreference("browser.download.folderList", 0);
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;");
//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");
//Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
prof.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"//MIME types Of MS Excel File.
+ "application/pdf;" //MIME types Of PDF File.
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document;" //MIME types Of MS doc File.
+ "text/plain;" //MIME types Of text File.
+ "text/csv"); //MIME types Of CSV File.
//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();