How to Use Robot Class in Selenium WebDriver for File Upload

File upload automation is a common requirement in web testing. Whether you’re testing a profile picture upload, document submission form, or any file-upload feature, Selenium WebDriver can handle it reliably with the right approach.

In this tutorial, you’ll learn:

  •  How file upload works in Selenium
  •  Different upload techniques (sendKeys, Robot class, AutoIt)
  •  Cross-browser examples in Java & Python
  •  Handling edge cases & best practices


Why File Upload Automation Is Challenging

Selenium WebDriver operates only within the browser’s DOM. However, most file upload dialogues are OS-level windows outside the browser scope. That’s why traditional click actions don’t always work. Selenium provides workarounds such as:

  • Using sendKeys() directly on file input elements

  • Using automation libraries like Robot (Java) or AutoIt (Windows) to interact with OS dialogs

  • Using advanced approaches for hidden or custom-styled inputs


Approach #1 – Using sendKeys() (Best & Simplest)

If the file selector element is an <input type="file">, Selenium allows us to upload a file by sending the absolute path to this element.

Why this method works

The browser loads the file path directly into the element without opening the OS dialog.


Java Example

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class FileUploadSendKeys { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("https://demo.demo.com/test/upload/"); // Locate the file input driver.findElement(By.id("uploadfile_0")) .sendKeys("C:\\Users\\YourUser\\Desktop\\file-to-upload.txt"); // Click submit driver.findElement(By.id("submitbutton")).click(); driver.quit(); } }

Python Example

from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("https://demo.demo.com/test/upload/") file_input = driver.find_element(By.ID, "uploadfile_0") file_input.send_keys(r"C:\Users\YourUser\Desktop\file-to-upload.txt") driver.find_element(By.ID, "submitbutton").click() driver.quit()

Key point: This only works when the element is a standard file input.


Approach #2 – Using Java’s Robot Class (When sendKeys Can’t Interact)

If clicking the upload button opens a system file dialog, Selenium alone cannot handle it. In such cases, we can use the Robot class in Java to type the file path and press Enter.

Java Example (Robot Class)

import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; import java.awt.event.KeyEvent; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class UploadWithRobot { public static void main(String[] args) throws Exception { WebDriver driver = new ChromeDriver(); driver.get("https://example.com/upload"); // Click upload button to open OS dialog driver.findElement(By.id("browseBtn")).click(); // Copy file path to clipboard String filePath = "C:\\Users\\YourUser\\Desktop\\file-to-upload.txt"; StringSelection selection = new StringSelection(filePath); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, null); Robot robot = new Robot(); // Ctrl + V robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_CONTROL); // Enter robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); driver.quit(); } }

Robot class simulates keyboard actions for OS dialogs that Selenium can’t directly control.


Notes on Other Tools

AutoIt (Windows Only)

AutoIt scripts can click and type into OS file dialogs. You can compile an AutoIt script and invoke it from your Selenium tests. Use this when Robot isn’t reliable.


Best Practices & Tips

i. Prefer sendKeys() When Possible

  • It's simple and reliable

  • Works across browsers without plugins

ii. Use Absolute File Paths

Always pass full paths to sendKeys(). Relative paths often fail.

iii. Hidden Elements?

Sometimes the upload button is a styled element, and input is hidden. In that case, directly target the hidden input via CSS or XPath.


Common Errors & Troubleshooting

ElementNotInteractableException

Occurs when you try to sendKeys() on an element that isn’t a file input.
✔ Use browser inspector to verify: should be input type="file".

sendKeys Opens File Explorer

If you incorrectly click the button instead of sending keys to the input, the OS dialog opens and automation halts. You should target the file input directly.


Summary

Method When to Use Works Without OS Dialog
sendKeys() Standard <input type="file"> ✔ Yes
Robot Class OS popup upload dialogs ❌ No
AutoIt (Windows) Complex windows workflows ❌ No

Final Thoughts

Automating file uploads in Selenium isn’t hard once you know which strategy to choose.

  • For most modern apps with standard file inputs, sendKeys() suffices.

  • For tricky dialogs or custom widgets, combine WebDriver with system automation tools like Robot or AutoIt.


Find Broken Links with Selenium  << Previous   |    Next >> Visual Testing with Selenium

Popular posts from this blog

Mastering Selenium Practice: Automating Web Tables with Demo Examples

18 Demo Websites for Selenium Automation Practice in 2026

Selenium Automation for E-commerce Websites: End-to-End Testing Scenarios

14+ Best Selenium Practice Exercises to Master Automation Testing (with Code & Challenges)

Best AI Tools for Automation Testing in 2026 (QA, SDET & Dev Teams)

Top 10 Highly Paid Indian-Origin CEOs in the USA

Automating Google Search with Selenium WebDriver: Handling AJAX Calls

Top Selenium Interview Questions & Answers of 2026

25+ Selenium WebDriver Commands: The Complete Cheat Sheet with Examples

Artificial Intelligence for Beginners (2026): Definition, Tools, and Real-World Impact