1

How to upload file using sendkeys & robot class?

I am attaching html & design to understand.

enter image description here

I have clicked on the highlighted area as shown in image & then i used this below code but doesn't work.

            Robot r = new Robot();
            r.keyPress(KeyEvent.VK_SLASH); 
            r.keyRelease(KeyEvent.VK_SLASH);
            r.keyPress(KeyEvent.VK_SHIFT);
            r.keyPress(KeyEvent.VK_U);
            r.keyRelease(KeyEvent.VK_SHIFT);
            r.keyRelease(KeyEvent.VK_U);
            r.keyPress(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_S);
            r.keyPress(KeyEvent.VK_E);
            r.keyPress(KeyEvent.VK_O);
            r.keyRelease(KeyEvent.VK_O);
            r.keyPress(KeyEvent.VK_PERIOD);
            r.keyRelease(KeyEvent.VK_PERIOD);
            r.keyPress(KeyEvent.VK_P);
            r.keyRelease(KeyEvent.VK_P);
            r.keyPress(KeyEvent.VK_D);
            r.keyRelease(KeyEvent.VK_D);
            r.keyPress(KeyEvent.VK_F);
            r.keyRelease(KeyEvent.VK_F);
            r.keyPress(KeyEvent.VK_ENTER);
            r.keyRelease(KeyEvent.VK_ENTER);

Just to give an idea i have used this code. So after this code executes & nothing happens nor file upload

I tried below code but didn't work for me

PageFactory.getWebDriver().findElement(By.xpath("//div[@class='cursor-pointer']")).click();
            StringSelection stringSelection = new StringSelection("/Users/faizan.mamji/Desktop/RER_Automation_Framework/TestingDoc.docx");
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
            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);

Faizan Mamji
  • 57
  • 1
  • 8

1 Answers1

0

So, the major issues with your code are:

  1. Since you are using a Mac machine Ctrl+V will not work, you need to use cmd+v instead.(use VK_META for CMD key).
  2. You need to provide specific permissions to your IDE to access the device. Please refer to this article for permissions:

Java.awt.Robot keyPress and keyRelease not working at all

  1. When you use robot after clicking on the upload button a Java app open and the browser loses focus(use CMD+TAB to get back to the previous app), Please use the below code as the same work in my case
        //click on button to open upload dialog
        driver.findElement(By.xpath("sample/xpath")).click();

        // Create a new Robot instance
        Robot robot = new Robot();
        Thread.sleep(2000);

        //File Need to be imported
        File file = new File("/Users/username/Documents/sampleFile.pdf");
        StringSelection stringSelection = new StringSelection(file.getAbsolutePath());

        //Copy to clipboard
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);

        // Cmd + Tab is needed since it launches a Java app and the browser looses focus
        robot.keyPress(KeyEvent.VK_META);
        robot.keyPress(KeyEvent.VK_TAB);
        robot.keyRelease(KeyEvent.VK_META);
        robot.keyRelease(KeyEvent.VK_TAB);
        robot.delay(500);

        //Open Goto window CMD+SHIFT+G
        robot.keyPress(KeyEvent.VK_META);
        robot.keyPress(KeyEvent.VK_SHIFT);
        robot.keyPress(KeyEvent.VK_G);
        robot.keyRelease(KeyEvent.VK_META);
        robot.keyRelease(KeyEvent.VK_SHIFT);
        robot.keyRelease(KeyEvent.VK_G);
        robot.delay(500);


        //Paste the clipboard value CMD+V
        robot.keyPress(KeyEvent.VK_META);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_META);
        robot.keyRelease(KeyEvent.VK_V);
        robot.delay(500);


        //Press Enter key to close the Goto window and Upload window
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.delay(500);

        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.delay(500);

        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.delay(500);


  1. Note that, after every key release you need to add some delay to make it work. Also, key combinations may vary on usecase.