Open In App

How to download File in Selenium Using java

Last Updated : 16 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Automating the file download process is essential in web automation testing, especially for validating functionalities involving document downloads such as PDFs, images, or CSV files. However, Selenium WebDriver doesn’t directly handle file downloads. To overcome this limitation, we can configure the browser’s download behavior, specifically using ChromeDriver, to automate file downloading in Java.

In this article, we’ll guide you through a simple, step-by-step process to automate downloading files using Selenium and Java. We’ll include practical examples to help beginners understand how to configure ChromeDriver, initiate the download, and verify the file has been downloaded correctly.

Example of how to download File in Selenium using Java:

Here is an example of how to download File in Selenium Using java:

Java
package com.example.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FileDownloadAutomation {
    public static void main(String[] args) {
        // Manually set the path to your local ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "C:/Users/HP/Desktop/Drivers/chromedriver_win32/chromedriver.exe");

        // Initialize the Chrome WebDriver
        WebDriver driver = new ChromeDriver();

        try {
            // Open URL
            driver.get("http://demo.automationtesting.in/FileDownload.html");

            // Enter text
            driver.findElement(By.id("textbox")).sendKeys("Hello world");

            // Generate Text File
            driver.findElement(By.id("createTxt")).click();

            // Click on Download Button
            driver.findElement(By.id("link-to-download")).click();

            // Print confirmation message to console
            System.out.println("File download has been triggered successfully.");

        } catch (Exception e) {
            // Print error message if something goes wrong
            System.out.println("An error occurred during file download: " + e.getMessage());
        } finally {
            // Close the driver after the actions are complete
            driver.quit();
        }
    }
}

Output

When the above code is executed, Selenium will automatically download the specified file from the URL provided to the directory you configured, without any pop-ups or manual intervention.

output
Output

Conclusion

Automating file downloads in Selenium with Java is crucial for testing applications that involve downloading documents or files. By configuring ChromeDriver with the correct preferences, you can bypass the manual download dialog and ensure files are downloaded to a specific location automatically. This approach is especially helpful for running tests in headless mode or in environments like CI/CD where human intervention is not possible.

By following the steps in this guide, you should be able to easily automate file downloads in Selenium using Java, improving the efficiency of your automation tests.


Next Article
Article Tags :

Similar Reads