Handle download confirmation popup/dialog in Firef

2019-04-16 03:35发布

问题:

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

回答1:

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.

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.