How to Download a File in Selenium Webdriver? [2021 Update]

How to download a file in selenium webdriver? Yes, sometimes we need to deal with this problem in our test automation projects. Also, there are test cases where you need to check if the download links are working fine or not. It’s not easy to automate them as mostly download features will work but content type might change. So, let’s learn some tips and tricks to download files or files in Selenium. :)

How to Download a File in Selenium Tool Stack?

We will use the below libraries to automate file download operations in selenium.

Step 1: Create WebDriver and Add Options

Chrome has many options that you can manipulate. First of all, you need to set the auto-download directory in ChromeOptions, then create the driver with that option. For this purpose, you need to set “download.default_directory” to a path that you desire. 

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", System.getProperty("user.dir"));

ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
WebDriver driver = new ChromeDriver(options);

Step 2: Download the file in Selenium

This is a custom part as you all have different scenarios. I assume that you all implement this part within your application. But just in case I add mine.

//We find the download links
List<WebElement> list =driver.findElements(By.cssSelector("div.module>p>a>img"));

//Click to 5MB web element
WebElement el = list.get(list.size()-1);
el.click();
Thread.sleep(500);

//Hide Google Popup Ad
js.executeScript("document.querySelector(\"html > ins\").style.display='none'");

//Again click to 5MB web element
el.click();

//Wait 15 seconds to download 5MB file.
//You can write custom wait. Check Selenium Wait article on swtestacademy.com
Thread.sleep(15000);

Step 3: Check The File in Folder

We create a Folder Object with the download path. Then get the file list on that folder and do a REGEX operation to check if the file is downloaded or not. Our regex is very hard coded, you might need to do some complex regex operation according to your needs.

File folder = new File(System.getProperty("user.dir"));

//List the files on that folder
File[] listOfFiles = folder.listFiles();
boolean found = false;
File f = null;
     //Look for the file in the files
     // You should write smart REGEX according to the filename
     for (File listOfFile : listOfFiles) {
         if (listOfFile.isFile()) {
              String fileName = listOfFile.getName();
               System.out.println("File " + listOfFile.getName());
               if (fileName.matches("5MB.zip")) {
                   f = new File(fileName);
                   found = true;
                }
            }
        }
Assert.assertTrue(found, "Downloaded document is not found");
f.deleteOnExit();

Now, you are able to download a file within a Selenium project and make the validation.

Note: In order to download many files, you can loop over all web elements and click each of them one by one.

GitHub Project

https://github.com/swtestacademy/SeleniumDownloadFile

Happy Testing!
Canberk Akduygu

2 thoughts on “How to Download a File in Selenium Webdriver? [2021 Update]”

    • In order to download many files, you can loop over all web elements and click each of them one by one. By the way, I have updated the project and pushed all codes to github.

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.