Find out when a download has completed using java & Selenium
Last Updated :
23 Jul, 2025
In Selenium automation, sometimes we may see some scenarios where we may need to test some download feature for example downloading recipt, invoices, img etc. Now the challenge is how to detect when a file download has been completed as Selenium itself doesnot handle any file downloads, the reason behind the downloaded file is saved outside the browser. But we can achieve our goal by monitoring the download folder for the expected file in Selenium.
In this post, we are going to learn How to detect a file download has been completed or not.
Downloading a File and Verifying Download Location
From downloading a file to verifying the download location if break this into steps, it would look like this -
1. Set up WebDriver
Change the path of "chromedriver.exe" according to your system chromedriver location. Selenium control the chrome browser with the help of chromedriver.exe.
System.setProperty("webdriver.chrome.driver", "D:\\path\\to\\chromedriver.exe"); // Update this path to your local ChromeDriver
// Create an instance of ChromeDriver
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();;
2. Navigate to the Download Page
Change the site address according to your requirement.
driver.get("https://the-internet.herokuapp.com/download");//Site will be change as per need
3. Locate and Click Download Link
findElement() method is used to locate element on a web page.The downloadLink.click() will trigger the download of the file "LamdaTest.txt".
WebElement downloadLink = driver.findElement( By.linkText("LambdaTest.txt")); // download file link
downloadLink.click(); // click on link
LamdaTest.txt4.Wait for the Download to Complete
Thread.sleep(5000);
It will pause the code execution for 5 seconds to complete the download.
5. Set the File Location for Verification
To find out when a file download is finished using Java and Selenium, this is the path where the browser will save the file the downloaded file. Set this path according to system.
File fileLocation = new File("C:\\Users\\foldername\\Downloads"); // set this according to your system
6. Verify the Download
After starting the download in the browser, the script will keep checking if the file shows up in that directory.
In the example, we will verify the existence of the file in a specific download format.
Downloaded File We can see that this file has a fixed name "LambdaTest.txt". We need to obtain this file path exactly so that we can find it among our existing files and verify its existence.
The below code iterates through the list of files in the download directory using a for loop.It will check if any file named "LamdaTest.txt" is present in the directory or not. If the file is present in the directory it will print "Success: 'LambdaTest.txt' has been downloaded successfully" and exit the loop.
File fileLocation = new File("C:\\Users\\foldername\\Downloads");
// Get the list of files in the directory
File[] totalFiles = fileLocation.listFiles();
// Check if the directory exists and contains files
boolean fileFound = false;
if (totalFiles != null) {
// Traverse through the files to find the downloaded file
for (File file : totalFiles) {
if (file.getName().equals("LambdaTest.txt")) {
fileFound = true;
System.out.println("Success: 'LambdaTest.txt' has been downloaded successfully.");
break;
}
}
6. Error Handling
However, if the file is not downloaded successfully, it will print "Error: 'LambdaTest.txt' was not found in the directory." Also, in case the directory is empty or does not exist, an error message will occur: "Error: The directory 'C:\\Users\\foldername\\Downloads' is empty or not found."
This way, the script knows when the download is done and can move on to the next steps.
Download_Check.java
package seleniumpractice;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Download_Check {
public static void main(String[] args) throws InterruptedException {
// Set up ChromeDriver manually by specifying the path
System.setProperty("webdriver.chrome.driver", "D:\\path\\to\\chromedriver.exe"); // Update this path to your local ChromeDriver
// Create an instance of ChromeDriver
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
// Open the URL
driver.get("https://the-internet.herokuapp.com/download");
// Locate and click on the download link
WebElement downloadLink = driver.findElement(By.linkText("LambdaTest.txt"));
downloadLink.click();
// Wait for the file to download
Thread.sleep(5000); // Increased sleep time to allow download to complete
// Specify the file download directory (default Downloads folder)
File fileLocation = new File("C:\\Users\\foldername\\Downloads");
// Get the list of files in the directory
File[] totalFiles = fileLocation.listFiles();
// Check if the directory exists and contains files
boolean fileFound = false;
if (totalFiles != null) {
// Traverse through the files to find the downloaded file
for (File file : totalFiles) {
if (file.getName().equals("LambdaTest.txt")) {
fileFound = true;
System.out.println("Success: 'LambdaTest.txt' has been downloaded successfully.");
break;
}
}
// If the file was not found, show an appropriate message
if (!fileFound) {
System.out.println("Error: 'LambdaTest.txt' was not found in the directory.");
}
} else {
System.out.println("Error: The directory 'C:\\Users\\foldername\\Downloads' is empty or not found.");
}
// Close the browser
driver.quit();
}
}
Output
Output of Find out when a download has completed using java & SeleniumConclusion
In summary, first the webdriver clicks on the download link, and saves the file in the download folder. Then the web driver looks out for the file at the given file location and matches the file and prints a successful message; otherwise, it will show an error message will occur: "the file was not found". With this approach, we can easily detect if the download feature is working or not.
With Selenium WebDriver and Java, automating this process improves the accuracy of your test cases and enhances the robustness of your automation framework.