0

I am trying to upload a file in selenium webdriver using Robot class and i am using Mac machine. Send keys doesnt work. Below is the code and it works fine when i load a file first time. I am trying to upload another file from the same page again but since the applet is open, no file is getting selected and script fails

browse.click(); // Click on a browse button from the page
File file = new File(photoLocation); //send path of the file

StringSelection StringSelection = new StringSelection(file.getAbsolutePath());

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(StringSelection, null); //Copies the filepath to clipboard


robot = new Robot();

//This launches java applet, so we are using cmd+tab to shift the 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
    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);

//Paste the clipboard value
    robot.keyPress(KeyEvent.VK_META);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_META);
    robot.keyRelease(KeyEvent.VK_V);

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

i am using the same code to be called again from the main page to upload the second file. But since the java applet is still open, i am unable to upload the file. Is there a way for me to close the applet?

3 Answers 3

2

I've tried so hard to do this. I finally found it. Instead of shifting focus with cmd+tab buttons, I used that:

driver.switchTo().window(driver.getWindowHandle());

Then, press CMD+SHIFT+G. Also, I added delay soon after pasting path.

    StringSelection StringSelection = new StringSelection(file.getAbsolutePath());
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(StringSelection, null);

    driver.switchTo().window(driver.getWindowHandle());

    Robot robot = new Robot();

    //Open Goto window
    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);

    //Paste the clipboard value
    robot.keyPress(KeyEvent.VK_META);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_META);
    robot.keyRelease(KeyEvent.VK_V);

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

I hope this helps Mac users to upload files.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me. Thank you so much. I struggled with this problem for a long time.
0

I tried for loop for upload and download multiple files-

File folder = new File("D:\\Test\\Documents");
    File[] files = folder.listFiles();
    String filesList = "";
    for(int i = 0; i < files.length; i++){
        filesList += (i != 0? "\n":"") + files[i].getAbsolutePath();
        StringSelection sel  = new StringSelection(filesList);
------------------------------------------------------------------------

but in out put first file always upload multiple times and files read correctly.

1 Comment

This is no answer, sorry. If you have a related question, ask a new one and refer here. Perhaps you wanted to comment on this question. Find related questions in menu "Related". To draw more attention on this question you can upvote it or start a bounty. If you haven't yet enough reputation to do so, it's for a reason---earn some!
0

Both the actions are performed twice because the actions are different.

You are pressing the meta key (Window key) and then the tab key. After reaching the desired window you are releasing both of them to land in that particular page, just like we do it in Alt+Tab.

That's why both the actions are performed twice.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.