2015-12-21 81 views
2

我想用这个selenium.For处理文件下载Firefox配置文件下载一个文件,我用下面的代码来设置Firefox的配置:无法通过设定使用硒的webdriver

FirefoxProfile profile = new FirefoxProfile(); 
    profile.setPreference("browser.download.folderList", 2); 
    profile.setPreference("browser.download.manager.showWhenStarting", false); 
    profile.setPreference("browser.download.dir", downloadPath); 
    profile.setPreference("browser.helperApps.neverAsk.openFile", 
      "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml"); 
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
    "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml"); 
    profile.setPreference("browser.helperApps.alwaysAsk.force", false); 
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false); 
    profile.setPreference("browser.download.manager.focusWhenStarting", false); 
    profile.setPreference("browser.download.manager.useWindow", false); 
    profile.setPreference("browser.download.manager.showAlertOnComplete", false); 
    profile.setPreference("browser.download.manager.closeWhenDone", false); 

通过我的UI,我正在下载2个文件。 第一个文件,我能够成功下载其弹出来是这样的:

enter image description here

但我无法下载第二个文件。对于第二个文件,弹出窗口如下所示: enter image description here

我不确定为什么我的Firefox配置文件设置无法处理第二个文件的下载。

请提出建议。任何帮助将不胜感激!

+0

似乎第一个文件是格式微软office的Excel 97-2003工作表和第二个是微软办公ExcelWorksheet (较新的版本),所以它无法找到开放的应用程序。试图通过点击保存然后尝试打开来保存它。 – Naruto

+0

@Naruto我相信这真的不容易做(甚至不可能)(点击保存) - 只使用硒。 – drets

+0

是否需要使用硒?或者我可以建议另一种方式? –

回答

0

使用Selenide,你可以尝试这样的事情,然后把它比作你在做什么:

@Test 
    public void userCanDownloadFile() throws FileNotFoundException, IOException 
    { 
    // Folder to store downloads and screenshots to. 
    reportsFolder = "./src/test/profiles/chrome/downloads/"; 

    open("http://chromedriver.storage.googleapis.com/index.html?path=2.16/"); 

    // Download files 
    $("a[href='/2.16/chromedriver_win32.zip']").download(); 
     $(By.xpath(".//a[@href='/2.16/chromedriver_mac32.zip']")).download(); 

     // Count files in folder, assert 2 
     int downloadsCount = new File(reportsFolder+"2.16").listFiles().length; 
     assertEquals("Should be 2 files but founded " + downloadsCount, 
       downloadsCount, 2); 

     // Clean after test 
     FileUtils.deleteDirectory(new File(reportsFolder+"2.16")); 
    }