1

I have a test to upload ,compress and download pdf files. Everything is working fine except the download part. I am not able to get how to handle the confirmation popup/Save or Open file dialog in Firefox when we click on download. I tried modifying firefox config but still not able to solve it. Can anyone please help?

FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.download.dir", "C:\\Users\\Sahil\\Downloads\\");
        profile.setPreference("browser.helperApps.neverAsk.openFile","application/pdf");
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");
        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);

        profile.setPreference("browser.download.manager.useWindow", false);
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
      , 0  profile.setPreference("pdfjs.disabled", true);
     WebDriver driver=new FirefoxDriver(profile);   
    driver.get("http://www.ilovepdf.com/compress_pdf");
    driver.findElement(By.id("pickfiles")).click();
    Runtime.getRuntime().exec("C:\\Users\\Sahil\\Documents\\Au\\Second.exe");
    WebDriverWait wait=new WebDriverWait(driver, 100);
    WebElement element1=wait.until(ExpectedConditions.elementToBeClickable(By.id("uploadfiles")));
    element1.click();

    WebElement element=wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a#download")));
    element.click();
2
  • 1
    why you use "browser.helperApps.neverAsk.openFile" along with "browser.helperApps.neverAsk.saveToDisk"? try to remove openFile preference
    – Andersson
    Jun 8, 2016 at 7:24
  • i am using both as firefox offers 2 options: Save File or Open with.. Tried by removing openFile but didn't help.. Jun 8, 2016 at 7:54

1 Answer 1

6

You have a lot of preferences that you do not need. All you need is this:

// Create a firefoxprofile for firefox specific settings
FirefoxProfile profile = new FirefoxProfile();

// Set the downloads folder
profile.setPreference("browser.download.dir", path/to/folder);

// Download files to the downloads folder
profile.setPreference("browser.download.folderList", 2);

// Don't show downloads window when download starts
profile.setPreference("browser.download.manager.showWhenStarting", False);

// Prevent file download dialog to be shown for certain MIME-types
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

If this doesn't work you should make sure you have correct MIME-type specified. You can do this monitoring the network in your browser and download the file manually. A GET request should be made with a specific content-type. Make sure that content-type has been added to your browser.helperApps.neverAsk.saveToDisk preference.

Edit! I just checked the MIME-type for the website you have specified. You are trying auto-saving a application/pdf. However you are downloading a file with MIME-type application/octet-stream. You need to change this:

profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

To this:

profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");

How to find the MIME-type

Before you hit the download button open your firefox console and go to the network tab. Firefox network tab

Now if you hit the download button you will see a GET request has been made. If you open this request you can see the content-type of the response. This is your MIME-type. MIME-type

2
  • Many Thanks, it worked.. Just one thing, can you please guide me where to get these MIME info..I am not so aware of these things Jun 8, 2016 at 11:09
  • @SahilSehgal I've added some explanation how to find it.
    – RemcoW
    Jun 8, 2016 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.