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.

Map<String, Object> prefsMap = new HashMap<String, Object>();
prefsMap.put("profile.default_content_settings.popups", 0);
prefsMap.put("download.default_directory", “Path of the download folder”);

ChromeOptions option = new ChromeOptions();
ption.setExperimentalOption("prefs", prefsMap);
option.addArguments("--test-type");
option.addArguments("--disable-extensions");

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.

package Test;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


public class DownloadFileDemo {

	public static void main(String[] args) throws InterruptedException {
		 System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");	
	  
		  String fileDownloadPath = "C:\\Users\\Avinash\\SeleniumDownload";
		  
		  Map<String, Object> prefsMap = new HashMap<String, Object>();
		  prefsMap.put("profile.default_content_settings.popups", 0);
		  prefsMap.put("download.default_directory", fileDownloadPath);
		  
		  ChromeOptions option = new ChromeOptions();
		  option.setExperimentalOption("prefs", prefsMap);
		  option.addArguments("--test-type");
		  option.addArguments("--disable-extensions");
		  
		  WebDriver driver  = new ChromeDriver(option);
		  driver.get("https://www.seleniumhq.org/download/");
	  	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  	  driver.manage().window().maximize();
	  	  Thread.sleep(2000);
	  	  driver.findElement(By.linkText("64 bit Windows IE")).click();
	  	  System.out.println("Downloaded");
	}

}

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