简体   繁体   中英

Unable to download a file by setting Firefox profile using Selenium Webdriver

I want to handle file download using selenium.For this, I used the following code to set the firefox profile:

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);

Through my UI, I am downloading 2 files. First file, I am successfully able to download whose pop up comes like this :

在此输入图像描述

But I am unable to download the second file. For the second file, the popup is like the one below: 在此输入图像描述

I am not sure why my firefox profile settings are not able to handle the download for the second file.

Kindly suggest. Any help would be highly appreciated!!

Using Selenide , you could try something like this and then compare it to what you are doing:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM