#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Selenium WebDriver

File uploading using Auto-It and Robot Class

The Problem

Native OS file-upload dialogs are not part of the browser's DOM, so WebDriver cannot directly interact with them using normal locators.

AutoIt Approach

AutoIt is a separate Windows scripting tool that can be used to write a small script that automatically interacts with the native file dialog, triggered from the Java test via a system command.

Robot Class Approach

Java's built-in Robot class can simulate keyboard actions (like typing a file path and pressing Enter) directly at the OS level once the file dialog opens.

Simplest Alternative

When the upload element is a standard <input type="file">, simply calling sendKeys(filePath) directly on it avoids the native dialog entirely and is the preferred approach whenever possible.

The Preferred Route: sendKeys on the File Input

Before reaching for OS-level tools, inspect the page. Many custom upload buttons still hide a standard file input underneath.

WebElement upload = driver.findElement(By.cssSelector("input[type='file']"));
upload.sendKeys("C:\\testdata\\resume.pdf");
driver.findElement(By.id("submit")).click();

This works in headless mode, in parallel runs and on any operating system. When tests run on Selenium Grid or a cloud provider, the file lives on your machine, not the remote node. Add a file detector so Selenium transfers the file first:

((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());

If the input is hidden with CSS, a developer can often expose it for test builds, which is far more reliable than automating the native dialog.

Using the Robot Class Step by Step

When the page truly opens a native dialog, Robot can type into it. Copying the path to the clipboard and pasting is more reliable than typing each character.

driver.findElement(By.id("browse")).click();   // opens the OS dialog

StringSelection path = new StringSelection("C:\\testdata\\resume.pdf");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(path, null);

Robot robot = new Robot();
robot.delay(1000);                       // let the dialog appear
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);
  1. Click the button that opens the dialog.
  2. Place the file path on the system clipboard.
  3. Wait briefly so the dialog has focus.
  4. Paste the path and press Enter.

On macOS the paste shortcut uses the Command key instead of Control, so the code needs a small platform check.

Using an AutoIt Script

AutoIt targets window controls by title and ID, so it does not depend on timing as much as Robot. A typical script, saved as upload.au3:

WinWaitActive("Open")
ControlFocus("Open", "", "Edit1")
ControlSetText("Open", "", "Edit1", $CmdLine[1])
ControlClick("Open", "", "Button1")

Compile it to upload.exe with the AutoIt compiler, then call it from Java after opening the dialog, passing the file path as an argument:

new ProcessBuilder("C:\\tools\\upload.exe", "C:\\testdata\\resume.pdf")
        .start()
        .waitFor();

The window title differs by browser and language, so check it with the AutoIt Window Info tool. Keep the compiled executable in a tools folder under version control so every machine in the team uses the same script.

Comparing the Options and Avoiding Failures

MethodWorks headless or on GridOperating systems
sendKeysYesAll
Robot classNo, needs a visible desktopWindows, macOS, Linux with a display
AutoItNoWindows only
  • Never lock or move the mouse while Robot or AutoIt tests run; they act on whatever window has focus.
  • Keep test files inside the project and build paths from the project folder rather than hard-coding drive letters.
  • After uploading, assert something visible, such as the file name shown on the page, rather than assuming success.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course