84

Is there any kind of API that can allow me to manipulate a file download dialog in Firefox? (I want to access the one that appears when user does something, not initiate one myself).

What I want to do is to access this dialog from Selenium (and whether Selenium "privileged mode" is enough to access chrome interface is something I am not sure about as well).

Andrey Shchekin
  • 21,101
  • 19
  • 94
  • 162
  • 4
    I’ve just spent the best part of three weeks configuring my own Apache virtual private server for the first time (because it’s a bit tricky running Selenium on shared hosting), getting Firefox, Selenium and Python working together, writing actual Python code to step through a very JavaScript-heavy site, all to download a file at the end of it. I then realised I had no idea how to actually access the downloaded file. I’m really glad you asked the question first. – Paul D. Waite Apr 04 '11 at 22:38
  • AppleScript would be great for that, if Firefox had a decent AppleScript dictionary. – Nicolas Barbulesco Nov 20 '13 at 15:58

11 Answers11

74

I have a solution for this issue, check the code:

FirefoxProfile firefoxProfile = new FirefoxProfile();

firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir","c:\\downloads");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");

WebDriver driver = new FirefoxDriver(firefoxProfile);//new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);

driver.navigate().to("http://www.myfile.com/hey.csv");
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
dlopezgonzalez
  • 4,217
  • 5
  • 31
  • 42
  • 1
    Close, but not quite working for Firefox on Ubuntu 12.04 as of 8/17/2013. Change the value of the "browser.helperApps.neverAsk.saveToDisk" property to "text/csv,application/vnd.ms-excel" and it will work on Ubuntu. – bakoyaro Aug 17 '13 at 13:07
  • 2
    May be the value name changed, this is since more than one year. – dlopezgonzalez Aug 21 '13 at 15:31
  • 1
    I am using the same method to auto-save the file but its not working. My firefox version is 20.0. And I am trying to download pdf files(MIME type used->application/pdf,application/x-pdf) – Yatin Apr 01 '15 at 06:50
  • 4
    Not that this is not working with `Content-Disposition: attachment`. Firefox seems to always popup a dialog in this case! – Martin Höller Jan 25 '16 at 12:12
  • for downloading xls files in linux this worked for me: `setPreference("browser.helperApps.neverAsk.saveToDisk", "application/xls");` – donfuxx Jun 02 '16 at 11:42
  • With current version of Firefox you have to change `browser.download.manager.showWhenStarting` by `browser.download.panel.shown`. – Augustin Laville Jun 29 '17 at 15:01
46

I was stuck with the same problem, but I found a solution. I did it the same way as this blog did.

Of course this was Java, I've translated it to Python:

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir",getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")

browser = webdriver.Firefox(firefox_profile=fp)

In my example it was a CSV file. But when you need more, there are stored in the ~/.mozilla/$USER_PROFILE/mimeTypes.rdf

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Akendo
  • 2,299
  • 2
  • 18
  • 16
  • 3
    I have tried this, but it doesn't seem to work for me. The file save dialog still shows up. – ealfonso Jun 21 '13 at 05:48
  • 1
    Thanks, the last preference was clutch: "application/pdf" – sam-6174 Feb 23 '15 at 00:05
  • 1
    I am using the same method to auto-save the file but its not working. My firefox version is 20.0. And I am trying to download pdf files(MIME type used->application/pdf,application/x-pdf) – Yatin Apr 01 '15 at 06:52
  • 1
    Thank you @Akendo. This is working for me using python 3.4.3, selenium-2.52 and Firefox 44. – Igor Feb 17 '16 at 19:38
33

Not that I know of. But you can configure Firefox to automatically start the download and save the file in a specific place. Your test could then check that the file actually arrived.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 1
    But selenium will launch Firefox in default profile (Since no profile is specified) so every time you have configure it. Instead of it create a new profile with your preferences so it is easier. – Vignesh Dhamodaran Mar 08 '16 at 19:13
10

Web Applications generate 3 different types of pop-ups; namely,

 1| JavaScript PopUps
 2| Browser PopUps
 3| Native OS PopUps [e.g., Windows Popup like Upload/Download]

In General, the JavaScript pop-ups are generated by the web application code. Selenium provides an API to handle these JavaScript pop-ups, such as Alert.

Eventually, the simplest way to ignore Browser pop-up and download files is done by making use of Browser profiles; There are couple of ways to do this:

  • Manually involve changes on browser properties (or)
  • Customize browser properties using profile setPreference

Method1

Before you start working with pop-ups on Browser profiles, make sure that the Download options are set default to Save File.

(Open Firefox) Tools > Options > Applications

enter image description here

Method2

Make use of the below snippet and do edits whenever necessary.

FirefoxProfile profile = new FirefoxProfile();

String path = "C:\\Test\\";
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", path);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);  
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.closeWhenDone", true);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
profile.setPreference("pdfjs.disabled", true);
       
driver = new FirefoxDriver(profile);
Community
  • 1
  • 1
Prashanth Sams
  • 19,677
  • 20
  • 102
  • 125
4

Most browsers (in mine case Firefox) select the OK button by default. So I managed to solve this by using the following code. It basically presses enter for you and the file is downloaded.

Robot robot = new Robot();

// A short pause, just to be sure that OK is selected
Thread.sleep(3000);

robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
ol_v_er
  • 27,094
  • 6
  • 48
  • 61
shish
  • 893
  • 1
  • 12
  • 20
4

I was facing the same issue. In our application the instance of FireFox was created by passing the DesiredCapabilities as follows

driver = new FirefoxDriver(capabilities);

Based on the suggestions by others, I did my changes as

FirefoxProfile firefoxProfile = new FirefoxProfile();     
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
    "application/octet-stream");
driver = new FirefoxDrvier(firefoxProfile);

This served the purpose but unfortunately my other automation tests started failing. And the reason was, I have removed the capabilities which were being passed earlier.

Some more browsing on net and found an alternate way. We can set the profile itself in the desired Capabilities.

So the new working code looks like

DesiredCapabilities capabilities = DesiredCapabilities.firefox();

// add more capabilities as per your need.
FirefoxProfile firefoxProfile = new FirefoxProfile();        
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
    "application/octet-stream");

// set the firefoxprofile as a capability
capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
driver = new FirefoxDriver(capabilities);
budi
  • 6,351
  • 10
  • 55
  • 80
Sanjay Bharwani
  • 3,317
  • 34
  • 31
3

Dont know, but you could perhaps check the source of one of the Firefox download addons.

Here is the source for one that I use Download Statusbar.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Nifle
  • 11,745
  • 10
  • 75
  • 100
  • Thanks, that very interesting (but proves that is either impossible or insanely hard unless I am writing an extension with xul parts). – Andrey Shchekin Jul 24 '09 at 22:31
3

I had the same problem, I wanted no access of Save Dialogue.

Below code can help:

    FirefoxProfile fp = new FirefoxProfile();
    fp.setPreference("browser.download.folderList",2);
    fp.setPreference("browser.download.manager.showWhenStarting",false);
    fp.setPreference("browser.helperApps.alwaysAsk.force", false);
    // Below you have to set the content-type of downloading file(I have set simple CSV file)
    fp.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");

According to the file type which is being downloaded, You need to specify content types.

You can specify multiple content-types separated with ' ; '

e.g:

    fp.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv;application/vnd.ms-excel;application/msword");
Pratik Patel
  • 2,209
  • 3
  • 23
  • 30
2

Instead of triggering the native file-download dialog like so:

By DOWNLOAD_ANCHOR = By.partialLinkText("download");
driver.findElement(DOWNLOAD_ANCHOR).click();

I usually do this instead, to bypass the native File Download dialog. This way it works on ALL browsers:

String downloadURL = driver.findElement(DOWNLOAD_ANCHOR).getAttribute("href");
File downloadedFile = getFileFromURL(downloadURL);

This just requires that you implement method getFileFromURL that uses Apache HttpClient to download a file and return a File reference to you.

Similarly, if you happen to be using Selenide, it works the same way using the built-in download() function for handling file downloads.

djangofan
  • 28,471
  • 61
  • 196
  • 289
  • That'll work only for files statically linked in the html, e.g. not a fille being generated based off a post (or similar) request. – Todor Minakov Dec 02 '18 at 10:54
  • Yeah, milage may vary. Usually, you'll be able to work with your devs to figure something out in those fringe cases. – djangofan Dec 04 '18 at 01:01
0

I didnt unserstood your objective, Do you wanted your test to automatically download file when test is getting executed, if yes, then You need to use custom Firefox profile in your test execution.

In the custom profile, for first time execute test manually and if download dialog comes, the set it Save it to Disk, also check Always perform this action checkbox which will ensure that file automatically get downloaded next time you run your test.

0

In addition you can add

      profile.setPreference("browser.download.panel.shown",false);

To remove the downloaded file list that gets shown by default and covers up part of the web page.

My total settings are:

        DesiredCapabilities dc = DesiredCapabilities.firefox();
        dc.merge(capabillities);
        FirefoxProfile profile = new FirefoxProfile();
        profile.setAcceptUntrustedCertificates(true);
        profile.setPreference("browser.download.folderList", 4);
        profile.setPreference("browser.download.dir", TestConstants.downloadDir.getAbsolutePath());
        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, data:image/png, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.download.manager.focusWhenStarting", false);
        profile.setPreference("browser.download.useDownloadDir", true);
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
        profile.setPreference("browser.download.manager.closeWhenDone", true);
        profile.setPreference("browser.download.manager.showAlertOnComplete", false);
        profile.setPreference("browser.download.manager.useWindow", false);
        profile.setPreference("browser.download.panel.shown",false);
        dc.setCapability(FirefoxDriver.PROFILE, profile);
        this.driver = new FirefoxDriver(dc);
Neil Ellis
  • 111
  • 1
  • 4
  • 1
    Hi Neil Ellis, Clicking on image button, "Export" opens a popup window. File download is triggered from the popup window and the popup is closed. If possible, please guide me in achieving silent file download in this scenario? – AVA Mar 10 '17 at 08:20