Back to All Scenarios
Scenario 40 of 100
File Upload/Download
Intermediate
Automating File Upload when OS Window Opens
π€Scenario Overview
Automating File Upload when OS Window Opens
Key Takeaways & Cheat Sheet
- βAcknowledge Selenium is blind to OS-level window dialogs
- βAvoid clicking the upload button which triggers the OS modal
- βLocate the hidden input type="file" element in the DOM tree
- βSend the absolute local file path directly using sendKeys()
Short Direct Answer
Selenium cannot interact with native OS dialogs (like the Windows file chooser). To automate uploads, bypass the OS popup entirely. Identify the hidden or functional `<input type="file">` element in the DOM and use `sendKeys()` to send the absolute local file path directly to it.
β οΈ Senior Warning (Red Flag)
Never click the upload button/wrapper (which triggers the OS file picker dialog) when automating. Selenium cannot interact with OS windows, causing your script to freeze and fail.
π‘ STAR Deep Dive Explanation & Pro Tip
This direct sendKeys approach is cross-platform compatible and works seamlessly in both desktop and headless CI environments.
SeleniumAutomation.java
Selenium 4 + Java// β
Best Practice: Send file path directly to <input type="file">
WebElement fileInput = driver.findElement(By.cssSelector("input[type='file']"));
// Send absolute path of local file (must exist on test runner)
String localFilePath = "C:\Users\vishv\Documents\test-report.pdf";
fileInput.sendKeys(localFilePath);