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);

6
  • Would be good if you add the code that you have tried so far ? Commented Feb 9, 2023 at 12:42
  • I have used robot class for this. Commented Feb 10, 2023 at 13:04
  • Attached code above, please guide me how to upload file so that i can successfully acheive. Commented Feb 10, 2023 at 13:16
  • I see no element where you want to upload the file in your code, I see no path of file in your code. This will help you stackoverflow.com/questions/9431978/… Commented Feb 10, 2023 at 13:36
  • I have did the same as you share the link. but not work for me. For file related thing i have tried but not shared here. Commented Feb 10, 2023 at 14:42

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @siddhartha agarwal, sure i will check & update here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.