Overview
Concrete tool for managing browser-downloaded files through a WebDriver session provided by the Selenium WebDriver library.
Description
HasDownloads is an interface that indicates a driver supports downloading remote files. It provides methods to check if downloads are enabled (via the se:downloadsEnabled capability), list downloaded files with metadata, download a specific file to a local path, and delete all downloadable files. The interface also includes an inner class DownloadedFile that encapsulates file metadata including name, creation time, last modified time, and size.
Usage
Import and use HasDownloads when you need to programmatically manage files downloaded by the browser during automation. The se:downloadsEnabled capability must be set to true in the driver options before downloads functionality can be used. Cast a WebDriver instance to HasDownloads to access download management methods.
Code Reference
Source Location
Signature
public interface HasDownloads {
default void requireDownloadsEnabled(Capabilities capabilities);
boolean isDownloadsEnabled();
static boolean isDownloadsEnabled(Capabilities capabilities);
@Deprecated List<String> getDownloadableFiles();
List<DownloadedFile> getDownloadedFiles();
void downloadFile(String fileName, Path targetLocation) throws IOException;
void deleteDownloadableFiles();
}
Import
import org.openqa.selenium.HasDownloads;
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| capabilities |
Capabilities |
Yes |
Used by requireDownloadsEnabled and the static isDownloadsEnabled to check for the se:downloadsEnabled capability
|
| fileName |
String |
Yes |
The name of the file to download (used by downloadFile)
|
| targetLocation |
Path |
Yes |
The local filesystem path where the file will be saved (used by downloadFile)
|
Outputs
| Name |
Type |
Description
|
| isDownloadsEnabled() |
boolean |
Returns true if the driver has the se:downloadsEnabled capability set to true
|
| getDownloadableFiles() |
List<String> |
(Deprecated) Returns a list of downloadable file names
|
| getDownloadedFiles() |
List<DownloadedFile> |
Returns a list of downloaded files with metadata (name, size, creation time, last modified time)
|
| downloadFile(fileName, targetLocation) |
void |
Downloads the specified file to the given local path; throws IOException on failure
|
| deleteDownloadableFiles() |
void |
Deletes all downloadable files from the remote browser session
|
DownloadedFile Inner Class
| Method |
Return Type |
Description
|
| getName() |
String |
Returns the file name
|
| hasExtension(extension) |
boolean |
Checks if the file name ends with the given extension (with or without leading dot)
|
| getCreationTime() |
long |
Returns the file creation timestamp
|
| getLastModifiedTime() |
long |
Returns the file last modified timestamp
|
| getSize() |
long |
Returns the file size in bytes
|
Usage Examples
// Enable downloads in ChromeOptions
ChromeOptions options = new ChromeOptions();
options.setCapability("se:downloadsEnabled", true);
WebDriver driver = new ChromeDriver(options);
// Navigate to a page and trigger a download
driver.get("https://example.com/files");
driver.findElement(By.id("download-button")).click();
// Manage downloaded files
if (driver instanceof HasDownloads) {
HasDownloads downloadsDriver = (HasDownloads) driver;
// Check if downloads are enabled
if (downloadsDriver.isDownloadsEnabled()) {
// List all downloaded files with metadata
List<HasDownloads.DownloadedFile> files = downloadsDriver.getDownloadedFiles();
for (HasDownloads.DownloadedFile file : files) {
System.out.println("File: " + file.getName() + " Size: " + file.getSize());
}
// Download a specific file to local path
downloadsDriver.downloadFile("report.pdf", Path.of("/tmp/downloads/report.pdf"));
// Clean up all downloaded files
downloadsDriver.deleteDownloadableFiles();
}
}
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.