File handling is essential in test automation when dealing with web applications that allow users to upload or download files. While Selenium WebDriver doesn’t provide direct support for file uploads or downloads, we can achieve this using different approaches.
File Upload in Selenium
There are multiple ways to upload files in Selenium, depending on the type of file upload element on the web page.
1. Using sendKeys() (Preferred & Easiest Method)
If the file upload field is an <input type="file">, we can use sendKeys() to pass the file path directly.
WebElement uploadElement = driver.findElement(By.id("fileUpload"));
uploadElement.sendKeys("C:\\Users\\User\\Documents\\testfile.txt");
This method is simple and effective — works 95% of the time!
2. Using Robot Class (For Native OS Dialogs)
When the upload button opens a native Windows/Mac file dialog (not accessible by Selenium), use the Robot class.
Robot robot = new Robot();
// Copy file path to clipboard
StringSelection filePath = new StringSelection("C:\\Users\\User\\Documents\\testfile.txt");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filePath, null);
// Paste (Ctrl+V) and Enter
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Useful when sendKeys() doesn’t work due to custom upload widgets.
3. Using AutoIT (Windows Only)
For complex upload dialogs, create an AutoIT script and execute it.
Runtime.getRuntime().exec("C:\\path\\to\\upload_script.exe");
Powerful but Windows-only and requires external script.
File Download in Selenium
By default, browsers show a download prompt — we can disable it using browser preferences.
1. Auto Download in Chrome (Most Used)
HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", "C:\\Downloads");
chromePrefs.put("safebrowsing.enabled", true);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
WebDriver driver = new ChromeDriver(options);
2. Auto Download in Firefox
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", "C:\\Downloads");
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/pdf,text/csv,application/octet-stream");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);
3. Verify File Download (Java)
// Wait for file to appear (optional with explicit wait)
File file = new File("C:\\Downloads\\report.pdf");
if (file.exists()) {
System.out.println("File downloaded successfully!");
} else {
System.out.println("Download failed!");
}
Best Practices Summary
- Use
sendKeys()→ 95% of file uploads (if input type=”file”) - Use Robot Class → When native dialog appears
- Use AutoIT → Only for complex Windows dialogs
- Always set download directory in Chrome/Firefox options
- Verify file existence after download
- Use absolute file paths (avoid relative paths)
Conclusion
- Use
sendKeys()for standard file uploads. - Use
Robot Classor AutoIT for handling OS file dialogs. - Set browser preferences for seamless file downloads.
- Verify downloaded files using Java.
This approach ensures effective handling of file uploads and downloads in Selenium automation.