How to Upload File in Selenium Webdriver (Handle Window Dialogs)

Selenium can't interact with Window diaologs on it's own

You all must know that Selenium can only handle browser windows and not OS windows. So, when we have to upload any file on a webpage and as soon as we click on the Browse or Upload button, a windows tab opens up from which we have to select the file. We cannot handle that windows tab directly with Selenium Webdriver.

But thankfully we can handle it by integrating Selenium with some other tools. One such tool is Robot Class.

What is Robot Class?

Robot Class is used to perform Keyboard actions. It has direct support to perform actions like press and release key. Robot class has key codes for each key present on the keyboard. So, if we want to press any key, then we need to call that key code.

In automation we make use of Robot class to handle Windows tabs by interacting with them via virtual keyboard. One important thing to keep in mind that, in robot class keyPress and keyRelease functions come in a combo. So, if you are pressing a key then you have to call release key function after that.

Use Cases of Robot Class in Selenium Automation:
  • Handle Upload File Windows dialog and select the file to be uploaded
  • Handle Download File Window Dialog and click on Save or Cancel file button

Scenario 1 - Handle Upload File Windows dialog and select the file to be uploaded

Test Steps:
  1. Go to File Upload Page.
  2. Click on the Upload file button.
  3. Select the file or copy the path of the file. (Windows dialog)
  4. And click on 'Ok' button. (Windows dialog)
From above steps we can execute first two steps using selenium and will perform 3rd and 4th step using Robot class.

Practical Code to upload image using Robot Class:

package com.techlistic.selenium;

import java.awt.AWTException;
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.firefox.FirefoxDriver;

public class FileUpload {

 public static void main(String[] args) {

  // Launch Browser
  WebDriver driver = new FirefoxDriver();

  // Maximize Window

  // Open URL 

  // Click on Browse button

  // Wait for 3 seconds
  try { Thread.sleep(3000);}
  catch (InterruptedException e){ e.printStackTrace();}

  // Get Location of the file to be uploaded 
  String fileLocation = System.getProperty("user.dir") + "\\test data\\" + "sample-upload-file.docx";
  StringSelection filepath = new StringSelection(fileLocation);

  // Copy the file path
  Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filepath, null);

  // Try block
  try {
   // Create object of Robot class
   Robot robot = new Robot();

   // Press Ctrl key
   // Press Ctrl + V key - It will paste the file path in windows dialog  

   // Now release V + Ctrl key

   // Click Enter Key 

   // Release Enter Key

  } catch (AWTException e) {e.printStackTrace();}

Take Screenshot  << Previous      ||      Next >>  25 Must to Know Selenium Commands 

Refer Selenium Webdriver Tutorials Series

Join Telegram channel at
Feel free to ask queries or share your thoughts in comments or email me.

Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.


Popular posts from this blog

10 Best Demo Websites for Automation Testing Practice

Automate 'User Registration' of E-commerce Website using Selenium

Automate E-Commerce Website using Selenium Webdriver

Automate Browser Actions on using Selenium