2015-12-21 84 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")); 
    }