How to Verify File Download in Selenium Webdriver Java

Selenium is an open-source web-based automation tool that is implemented using a web driver. We will be using geckodriver because Selenium 3 enables geckodriver as the default WebDriver implementation for Firefox.


  1. geckodriver.exe
  2. maven dependency selenium


The following 9 steps will automate the process:

  1. Set webdriver.gecko.driver and its' path as a system property.
  2. Set the firefox diver.
  3. Typecast web driver as Javascript executor. 
  4. Browse the website.
  5. Get the download button and click then wait to start downloading.
  6. Open the downloads section of the browser using the web driver.
  7. Get the file name of the most recent file by executing the following script containing the javascript path:
  8. return document.querySelector('#contentAreaDownloadsView .downloadMainArea .downloadContainer description:nth-of-type(1)').value
  9. Wait for the file to be downloaded.
  10. Check from the downloads local directory, if that file exists.
Let’s see all the above steps in the code. We will use js.executeScript(String script) function to get file name for verification.
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.sql.Time;
import java.time.Duration;
import java.util.Random;

public class VerifyFileDownload {
    public static String GECKODRIVER_PATH = "F:\\WORK\\SeleniumShortTasks\\VerifyFileDownload\\src\\main\\resources\\resources\\geckodriver.exe";
    public static Random r = new Random();

    public static void main(String[] args) {

        System.setProperty("webdriver.gecko.driver", GECKODRIVER_PATH);
        WebDriver driver = new FirefoxDriver();
        JavascriptExecutor js = (JavascriptExecutor) driver;

        //get the firefox browser & get the file downloaded
        String siteLink = "";

        //Get Download Button
        WebElement downloadButton = driver.findElement(By.cssSelector(""));;

        //wait , until file started downloading  : 7-9 sec
        System.out.println("wait, until file started downloading");
        threadWait(7, 9);

        //open the downloads section of firefox

        //get the most recent file name
        String fileName = (String) js.executeScript("return document.querySelector('#contentAreaDownloadsView .downloadMainArea .downloadContainer description:nth-of-type(1)').value");

        //wait, expecting file to be downloaded , after 15-20 sec :
        System.out.println("wait, expecting file to be downloaded.");
        threadWait(15, 20);

        //verify from Downloads Directory is this file exists
        String FILES_DIRECTORY = "C:\\Users\\Momin-PC\\Downloads";
        File Folder = new File(FILES_DIRECTORY);
        File[] allFiles = new File(Folder.getPath()).listFiles();
        for (File file : allFiles) {
            String eachFile = file.getName();
            if (eachFile.contains(fileName))
                System.out.println("--Verified: File : " + fileName + " Has Been Download.");
            else continue;


    public static void threadWait(int low, int high) {
        int num = getRandomNumber(low, high);
        System.out.println(num + " sec Thread Wait : Started");
        try {
            Thread.sleep(1000 * num);
        } catch (Exception e) {
        System.out.println(num + " sec Thread Wait : Ended");


    public static int getRandomNumber(int low, int high) {
        int num = r.nextInt(high - low) + low;
        return num;


All resources used in this tutorial are attached:
  • source code
  • geckdriver.exe