πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
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);