How to Download File using WebDriver

This is Part 9 of the Selenium WebDriver Tutorial Series. Check out Part 8 to know about File Upload using WebDriver.

When we download file from a web page, it asks for confirmation to download along with the download location. For automating this download file process, we have to set some preferences based on the browser used.

In this article, we will see file download for Firefox and Chrome separately.

Download File using FirefoxDriver

For firefox, we have to set the below firefox profile preferences.

  • browser.download.dir – The download directory
  • browser.download.folderList – It has to be set to 2 to make firefox use custom download directory (the above given download directory). If value is 0, it downloads the file to user’s desktop and if its 1, it uses the default Downloads folder.
  • browser.download.manager.showWhenStarting – Whether to show the download manager when starting download
  • browser.helperApps.neverAsk.saveToDisk – Never ask for saving to disk for the given file types.

Using the above preferences, we can create the profile and initialize the FirefoxDriver.

Download File using Chrome

The method is similar for chromedriver in which we set the ExperimentalOptions preferences in ChromeOptions as shown below.

Preferences Set

  • download.default_directory – The download directory
  • profile.default_content_settings.popups – To disallow popups
We will be covering How to Upload Files using WebDriver in a future post.
Further Reading:

SEE ALSO:   How to Run TestNG xml Programmatically

You may also like...

Leave a Reply

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