How to download file in Selenium with Chrome browser?

In the previous tutorial, we discussed the technique to upload file in Selenium, but by using AWT and Robot class. I would like to inform you that Robot class is not a part of Selenium. Today we will use Selenium Library to download file for your Selenium project. There are certain pre-requisites that you should know before we discuss the Selenium’s inbuilt feature to download file at runtime. Those are about Capabilities, setting preferences and browser-based options. If you do not know them then I would suggest you quick articles on these topics. Click on the link below to learn them in a much more detailed manner.

Apart from these knowledge banks, if you want to learn the difference between Functional testing and Non-Functional testing then click on the link below:

These are some of the suggested tutorials which you should read before we jump to the main discussion about downloading a file in Selenium at runtime.

Core Concept behind File download technique of the Selenium

We are initially handling the download confirmation notification which we get in Google chrome by setting its preferences to zero. After handling the download notification, we set the path of the desired download folder where the downloaded file will be stored. We are putting these settings to a hash map.

Further, we are setting the above discussed preference map to the setExperimentalOption() method of the ChromeOptions class. In the end, we can include other arguments in our code based on the requirement. This explanation is directly related to our coding, which we will see in later part of this tutorial.

How to implement the download file facility in Selenium project?

Now coming to the implementation part. We just need seven lines of code to accommodate the download file feature in our Selenium project. Here is the sample code.

From the above code- profile.default_content_settings.popus used to handle the download notification bar and download.default_directory used to set the default or desired path of our downloaded file. The remaining code is about setting the chrome options. Hope you read the tutorial above on Chrome options.

Download File Test Scenario

We will see the complete code which is used to download file in Selenium. Here we first open the Selenium official website and go to the download page then WebDriver finds the IE file to download by using link text, then it clicks over there and finally we see the downloaded file to our desired folder.

Here is the sample program for you.

Download File in Selenium

This was all about the technique to download file in Selenium without using any third-party tool. If you have any queries, then you can write in a comment below and don’t miss to join our Facebook group for the latest updates.

Join Inviul fb group

Leave a Reply