Skip to main content

How To Download a File

The Problem

Just like with uploading files we hit the same issue with downloading them -- a dialog box just out of reach of your automation script. With some additional configuration, we can side-step the dialog box.

A Solution

In order to avoid the dialog box, we will be instructing the browser to download files to a specific location without being prompted. After the file is downloaded we can then perform some simple checks to make sure the file is what we expect.

Let's continue with an example.

Example

Expected Behavior

After executing the test, this is what will happen:

  • Create a uniquely named temp directory in the present working directory
  • Open the browser
  • Visit the page
  • Find and click the first download link on the page
  • Automatically download the file to the temp directory without prompting
  • Check that the temp directory is not empty
  • Check that the downloaded file is not empty
  • Close the browser
  • Delete the temp directory

Outro

A similar approach can be applied to some other browsers with varying configurations. But downloading files this way is not sustainable or recommended. Mark Collin articulates this point well in his prominent write-up about it. In a future tip we will cover a more reliable, faster, and scalable browser-agnostic approach to downloading files. Stay tuned.

Happy Testing!

Special Thanks

Mike Millgate
Contributed the Python code for this tip.
Isaul Vargas
Python code review
Peter Bittner
Python code review
Jonathan Taylor
Contributed the initial C# code for this tip.
Roman Isko
Contributed the initial Java code for this tip.
Andrei Solntsev
Contributed the Selenide code for this tip.

Authors

Dave Piacente
Original creator of Elemental Selenium
Diego Molina
Current maintainer of Elemental Selenium