Selenium Webdriver Tutorials

Download a file with Selenium Webdriver using Firefox Profile

While scripting you might encounter a scenario which requires the automation script to download a file with Selenium Webdriver (say MS Excel, MS Word, Zip file, PDF, CSV, Text file) from web application. What happens when you click on ‘Download’? Yeah! A pop-up window is displayed asking user to either open, save or ‘save as’ the file. If you have noticed, this is NOT a browser HTML pop-up but Windows OS pop-up. And Selenium Webdriver is a ‘Web Browser’ automation tool, i.e. it works only in the browser. We cannot access operating system’s native windows with Selenium Webdriver. Ooops! A blocker! Does this mean we cannot automate the File download scenario? Nah! It simply means we need to find a workaround 😉 In this article let’s explore how to download a file with Selenium Webdriver using Firefox Profile.

Did you notice we have specifically mentioned ‘Mozilla Firefox browser’ for this article? That’s because different browsers have different behaviors when it comes to file download. Internet Explorer will always show the system popup with Open, Save, Save As options. Firefox and Chrome will vary depending on the settings – it either directly start downloading the file by default OR displays the system pop-up. How to download a file with Selenium Webdriver depends on the browser in question. For this article, we have restricted it to ‘Mozilla Firefox’, will cover ‘Google Chrome’ in future articles.

What is MIME Type?

MIME stands for Multi-purpose Internet Mail Extensions. MIME types form a standard way of classifying file types on the Internet. Internet programs such as Web servers and browsers all have a list of MIME types, so that they can transfer files of the same type in the same way, no matter what operating system they are working in. A MIME type has two parts: a type and a subtype separated by a slash (/). For example,

  • Text File (.txt): text/plain
  • PDF File (.pdf): application/pdf
  • CSV File (.csv): text/csv
  • MS Excel File (.xlsx): application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • MS word File (.docx): application/vnd.openxmlformats-officedocument.wordprocessingml.document

Before downloading any file, it is required to pass the MIME type of the file to the Firefox profile.

Firefox Profile Preferences

To download a file with Selenium Webdriver, we need to do some settings to the browser using Firefox profile preferences, so that it automatically downloads the file to the defined folder. Then we can write code to check if the file is downloaded or not.

setPreference(“browser.download.folderList”, 2);

  • 0 – Default. Save file on the user’s desktop
  • 1 – Save file in the Downloads folder
  • 2 – Save file at the location specified for the most recent download.

setPreference(“browser.download.manager.showWhenStarting”, bool);

Default is True. It specifies whether the Download Manager window will be displayed when a file download is initiated.

setPreference(“browser.helperApps.alwaysAsk.force”, bool);

Default is True. Always ask what to do with an unknown MIME type.

setPreference(“browser.helperApps.neverAsk.saveToDisk”,value);

Default is empty string. A comma-separated list of MIME types to save to disk without asking what to use to open the file.

setPreference(“browser.helperApps.neverAsk.openFile”,value);

Default Value is empty string. A comma-separated list of MIME types to open directly without asking for confirmation.

setPreference(“browser.download.dir”,path);

Default value is empty string. The directory path to save the file.

setPreference(“browser.download.manager.alertOnEXEOpen”,bool);

Default is True. True – warn the user attempting to open an executable, False – displays no warning and allow executable to be run.

setPreference(“browser.download.manager.closeWhenDone”,bool);

Default is False. True closes the Download Manager when all downloads are complete.

setPreference(“browser.download.manager.focusWhenStarting”,bool);

Default is False. True – Set the Download Manager window as active when starting a download, False – leaves the window in the background when starting a download.

Download a file with Selenium Webdriver

This script will download Adobe Reader from Filehippo.com

Note: Browser profile preferences is NOT supported by Internet Explorer. As such, there is no way to automatically download files to a specified location with Internet Explorer / Edge Browser.

If you run above code files will be download automatically and download window will not appear 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *