How to upload file in Selenium WebDriver using Robot class

Upload file using Robot class –

Suggested post to read –  How to upload file in Selenium WebDriver using sendKeys() method

In this article, we will see how can we upload file in Selenium WebDriver using robot class.

Upload file in Selenium WebDriver using ROBOT Class

This is another way using which we can upload file. Here we can do key press and key release using robot class.

This method is for handling the Windows File Upload dialog, which cannot be handled using Selenium. Please follow below steps:

1.   Click on the File Upload / Choose File button, so that the File Upload dialog is displayed.
        driver.findElement(By.xpath(“//*[@id=’uploadfile_0′]”)).click();

2. Copy your file’s absolute path to the clipboard
    StringSelection ss = new StringSelection(” C:UsersPrakashDesktopDesktopTOSCA tutorial.pptx”);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

3. Paste the file’s absolute path into the File name field of the File Upload dialog box

    //native key strokes for CTRL, V and ENTER keys

    Robot robot = new Robot();
    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);

Refer below code –

package basicsOfSelenium;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class FileUploadInSelenium_UsingRobotClass {

 public static void main(String[] args) throws AWTException {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("http://demo.guru99.com/selenium/upload/");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  //open upload window
     driver.findElement(By.xpath("//*[@id='uploadfile_0']")).click();

     //put path to your image in a clipboard
     StringSelection ss = new StringSelection("C://Users//Prakash//Desktop//Desktop//TOSCA tutorial.pptx");
     Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

     //imitate mouse events like ENTER, CTRL+C, CTRL+V
     Robot robot = new Robot();
     robot.delay(250);
     robot.keyPress(KeyEvent.VK_ENTER);
     robot.keyRelease(KeyEvent.VK_ENTER);
     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.delay(90);
     robot.keyRelease(KeyEvent.VK_ENTER);

  
 }

}

Refer below articles to learn more about Actions class in Selenium WebDriver –

Hope this helps !!

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories