无法通过使用Selenium Webdriver设置Firefox配置文件来下载文件

时间:2015-12-21 17:39:27

标签: java selenium selenium-webdriver

我想使用selenium处理文件下载。为此,我使用以下代码设置firefox配置文件:

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配置文件设置无法处理第二个文件的下载。

请建议。任何帮助将非常感谢!!

1 个答案:

答案 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"));
    }