In this article we will see how we can download a pdf file using selenium at a desired location in java on Firefox browser.
Many applications generate PDF Report like Billing Report, Policy Report, so on and as part of the testing process, the QA needs to download the PDF’s and verify the data present in that report. We should be able to perform this operation using automation also. Selenium supports the downloading of the document like PDF, word, excel and many more.
Implementation Steps
Step 1 – Define directory path for PDF to be downloaded
Define the directory path where the PDF will be downloaded. Here, it will create a directory with the name of downloads in the project.
String downloadFilepath = System.getProperty("user.dir") + File.separator + "downloads";
Step 2 – Configure FirefoxOptions to set download preferences in Firefox
This includes setting the browser to always download PDFs instead of opening them in the browser, and specifying the default download directory.
FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.folderList", 2);
options.addPreference("browser.download.dir", downloadFilepath);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
Explanation
options.addPreference("browser.download.folderList", 2);
This preference sets where downloaded files will be saved. The value `2` indicates that all downloaded files should be saved to a user-specified directory.
options.addPreference("browser.download.dir", downloadFilepath);
This preference sets the directory path where Firefox will save the downloaded files.
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
This preference specifies the MIME types for which Firefox will automatically download files without prompting the user with an “Open/Save As” dialog. In this case, application/pdf is being used to indicate that PDF files should be downloaded directly.
Step 3 – Initialize WebDriver and Open Webpage
WebDriver driver = new FirefoxDriver(options);
driver.manage().window().maximize();
driver.get("https://freetestdata.com/document-files/pdf/");
Step 4 – Find and Click the Download Link and wait for the download completion
Locate the download link using XPath and click on it to start the download.
WebElement downloadLink = new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class=\"elementor-button-text\"]")));
downloadLink.click();
Step 5 – Verify the File Download
Check if the downloaded file exists in the specified download directory or not.
File downloadedFile = new File(downloadFilepath + "/Free_Test_Data_100KB_PDF.pdf");
if (downloadedFile.exists()) {
System.out.println("File is downloaded!");
} else {
System.out.println("File is not downloaded.");
}
Step 6 – Quit the WebDriver
driver.quit();
The complete program can be seen below:
package com.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.time.Duration;
public class FirefoxDownload_PDF {
public static void main(String[] args) {
// Setup download directory
String downloadFilepath = System.getProperty("user.dir") + File.separator + "firefox_downloads";
// FirefoxOptions configuration
FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.folderList", 2);
options.addPreference("browser.download.dir", downloadFilepath);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
// Initialize Firefox WebDriver and configure browser window
WebDriver driver = new FirefoxDriver(options);
driver.manage().window().maximize();
driver.get("https://freetestdata.com/document-files/pdf/");
// Locate the download link/button and click and wait for the download to complete
WebElement downloadLink = new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class=\"elementor-button-text\"]")));
downloadLink.click();
// Verify if the PDF file exists
File downloadedFile = new File(downloadFilepath + "/Free_Test_Data_100KB_PDF.pdf");
if (downloadedFile.exists()) {
System.out.println("File is downloaded from Firefox!");
} else {
System.out.println("File is not downloaded.");
}
// Cleanup: close the browser
driver.quit();
}
}
The output of the above program is

We can see that a firefox_downloads folder is created, and it has the downloaded pdf file – Free_Test_Data_100KB_PDF.pdf.

Summary:
- Setup: Configures Firefox to automatically download PDFs and sets the download directory.
- Automatically save files of specified MIME types (in this case, PDFs) without prompting the user with a dialog.
- Download and Verify: Navigates to a specific URL, clicks the download link, waits for the download to complete, and verifies if the file exists in the specified directory.
- Cleanup: Closes the browser session.
That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!