Advance Selenium

How to upload files in Selenium Webdriver in MAC OS

Upload file in Selenium in mac

File upload  and File Download in Selenium Webdriuver has become very common scenario nowadays. I have automated the same scenario in Windows machine so many times. I have used two ways to upload a file in Selenium in Windows Machine.We have robot class option to Upload file in Selenium in mac.

 

Please check out below article for the same.

Upload File using AutoIt

Upload File using Robot Class 

Download files in Selenium Webdriver

I got a couple of queries for Uploading files in Selenium in MAC OS. I have not used MAC till now so I was helpless and unable to answer.

Yesterday one of my friend Nithya she automated the same scenario and has given me solution for the same.

 

Steps to follow for Upload file in Selenium in mac

1. Click the upload button on your Webpage and ensure file upload pop-up appears.

2. Create a File object with the file path as an input and assign the absolute path of it to StringSelection object

3. Copy the path of the file in the clipboard by using getSystemClipboard method.

4. Bring GOTO window by pressing Command+Shift+G and assign the clipboard value.

5. Press Enter key to get GO button clicked from GOTO window.

6. Press Enter key to get Open button clicked from File Upload popup.

 

Upload file in Selenium in mac

Upload file in Selenium Webdriver in MAC OS

 

SAMPLE Code to Upload file in Selenium in mac

//Click on the Import Button

browser.findElement(By.className("import­button")).click();

Thread.sleep(2000);

//File Need to be imported

File file = new File("/Users/John/Desktop/IMG_0100.PNG");

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

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

Robot robot = new Robot();

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

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

 

Please try the same code from your end and let us know if it is working for you. If you have used any other way to handle the same scenario please let us know we will try to post the article on same.

Like,Share and Comment if above post works for you.

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

51 thoughts on “How to upload files in Selenium Webdriver in MAC OS

  1. Ashish says:

    Hi.
    I tried the same. The issue here is the file upload is working perfect with Safari. But when running on other browsers nothing happens. I am stuck with this now. Can you please help.
    @Mukesh

    1. Hi Ashish, you can also use sendKeys method directly if you want to upload files.

      1. Ashish says:

        Hi Mukesh,
        This issue is real,
        on MAC file upload using robot class works perfect with safari but doesn’t works with any other browser. Nothing happens.
        I have given all access the browsers but still no luck.
        Also can’t use sendKeys, as the input type is hidden.

        1. Hi Ashish,

          There are issues with Mac OS while using robot class.

  2. Hamid says:

    Please separate the two lines below for avoiding confusion between comment and code:

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

    1. Hi Hamid,

      Thanks for pointing out the formatting issue 🙂
      I’ll correct it

  3. Gokul says:

    Tried the code and its working .Thanks so much. One problem I am facing is, when I run the code in headless mode its not working. Any suggestions.

    1. Hi Gokul, please share logs and other details to mukeshotwani@learn-automation.com

  4. Bhanu Chaitanya says:

    command+Shift+G is pressing is not happening in my case. Once the dialog box is opened, it is navigating to ‘g’ folder instead of pressing go to window. Help me here.

  5. Jenia says:

    Focusing on upload file window doesn’t work for me… using macOS Big Sur.
    Do you have any suggestions?

    1. Hi Jenia, can u tell me what exactly the issue you are facing while working with robot class on MAC.

    2. Olu says:

      Hi Jenia,

      I had the same problem, but was able to fix it by giving permission to my IDE (in my case Intellij) to control my mac. Steps and link to follow below.

      Settings -> Security & Privacy
      Scroll down to “Accessibility”
      Click the lock button at the bottom and unlock the security and privacy preferences
      Click on ‘+’ icon and your program (Eclipse IDE in my case) and try running again

      https://stackoverflow.com/questions/53901026/java-awt-robot-keypress-and-keyrelease-not-working-at-all/54965052#54965052

      Also thanks @Mukesh for the solution

  6. payal says:

    Can you please share same program with python instead of java

  7. VAIBHAV PRABHUGAONKAR says:

    WORKS LIKE A CHARM. THANK YOU MUKESH OTWANI

    1. Thanks, Vaibhav…:)

  8. Mridul Chawla says:

    Hi Mukesh,

    Thanks man. Your answer is to the point. My script was not working but adding robot.delay(1000); in between keyRelease steps solved my problem. I am using Katalon studio and this script is working like a charm in macOS Catalina.

  9. Samreen Adil says:

    Hi Mukesh,

    Thank you so much for this useful article. I have implemented the above code, however last section to hit enter to close the goto window do not execute.

    Any idea why its happening this way?

    1. Hi Samreen,

      Since there is an involvement of robot class only over here, please check whether the focus is coming up on ‘Close’ button correctly or not.

  10. purushotham says:

    Hi Mukesh,

    I tried with the same code in my mac book but it is giving below error. it will open the particular folder but it is not selecting the file
    “2019-09-25 13:42:54.390 java[1796:83606] pid(1796)/euid(501) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!”

    1. Hi Purushotham,

      It looks like known issue. Please refer this link https://bugs.openjdk.java.net/browse/JDK-8226806

    2. Nikitha says:

      Hi ,

      Even i am geeting the same error in MAC OS Mojave , but in ma previous MAC same code was working fine.

      Did you get the solution?

      @Mukesh : If you could help with this , it will be really great.

      Thanks,
      Nikitha

      1. Hi Nikitha,

        As of now there is no resolution for this issue

        1. Shivam Gupta says:

          Hi Mukesh,
          I am getting same issue.
          did you get any update on this issue?
          I am blocking due to this. it would be great @Mukesh if you can help with this

          1. Hi Shivam,

            I need to check it.

    3. Mithun says:

      Facing the same issue

      1. Hi Mithun,

        From JDK bug link, it looks like Fix version has been mentioned as JDK 15

  11. Navi says:

    Hi Ravi, I’ve been using Robot class in my framework for a long time now but since i’ve updated my OS to Mojave (Mac ) there seems to be an issue :
    Error:
    2019-06-29 21:31:24.457 java[86582:7471898] pid(86582)/euid(502) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!

    Is there an alternate that can be used to upload the image from system ?

    1. Hi Navi,

      This seems to be issue from many people and looks like OS blocks keyboard operations. I haven’t updated my Mac OS. You can refer this link https://github.com/octalmage/robotjs/issues/424
      Why don’t you give try for Sikuli

  12. Hardy says:

    WebElement uploadElement = driver.findElement(By.xpath(“/html/body/div[1]/div[2]/div[2]/div/form/div/div[2]/div[1]/div”));
    uploadElement.click();

    driver.switchTo().activeElement().sendKeys(“*/Users/abc/Desktop/Contract.pdf”);
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    this is code after login used, in this open file path but unable to choose the file in Mac

    1. Hi Hardy,

      If you are clicking on some browse/upload button on web ui and want to pass file path in Select file window using Selenium then it won’t work. As Selenium doesn’t support OS based windows.

  13. Avanti says:

    Hi Mukesh, thanks for your code, it worked wonders in mac OS Sierra. But recently i had to upgrade my OS to Mojave for other technical dependencies and since then the upload testcase is not working anymore. I am not sure if anyone else has faced the same issue. Please help

    1. Hi Avanti, What is the issue is coming now?

      1. Avanti says:

        I have 8 images to be uploaded on the same page and i am using a for-loop to do so. For test purposes, i am uploading the same image for all the 8 upload slots. The problem is, selenium is not waiting for the image to be uploaded and moving on to the next upload button. As a result, by the end of 8 loops, only 4 files are uploaded and scripts fails. I have tried using explicit waits but in vain.

        1. Hi Avanti,

          Using selenium it is possible to check whether file has been uploaded fully or not because it depends on many factors such as your local network, server capacity and so on. You can try BrowserMobProxy to check responses for file uploaded successfully.

  14. Vishal Roy says:

    Have you built something like this for python as well ? If not, do you know how can I achieve this in any mac or linux OS with python ?

    1. Hi Vishal,

      For python, I don’t have such kind of video. But this link https://github.com/asweigart/pyautogui will help you to achieve your objective.

  15. Meet says:

    Hi Mukesh. I am a manual tester and I was showing efforts for going into automation but my manager wanted me to prove that I can automate. I was stuck since a long time for automating upload on Mac OS. Now only because of you and only you, I am able to do it. No where on internet I found solution but here. Thanks to you that you share info with the community. Thank you for your efforts. Only because of people like you someone can learn who don’t have money to spend on paid online courses. Please keep doing this.

    1. Hi Meet,

      Thanks alot for your overwhelming comments. Your comments made my day…:)

  16. Roman Glaz says:

    Great topic!!! thanks its works in my environment

    1. Hi Roman,

      You’re always welcome…:). Feel free to ask your doubts on my blog.

  17. Hi Mukesh, thank for this. I did modify the path to work on my codes, since the parameter I pass is just the filename.
    // uploadFile = test1.txt (param)
    String uploadFilePath = “/src/test/java/com/files/”;
    String projectDir = System.getProperty(“user.dir”);
    String directory = projectDir + uploadFilePath + uploadFile;

    StringSelection media = new StringSelection(directory);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(media, null);

    Then I applied your codes below, and it works!

  18. Nishchay Gupta says:

    Hi Ravi,

    Your code is working but not completely for me. It selects the file but does not select the open button for attaching the file. What lines need to be added then ?

    1. Try with some other keyboard event using Robot class.

  19. ravi550 says:

    Hi Mukesh: This is Ravi again. I tried your code and working fine. However, I noticed that Java app window is not closing until Browser is closed. This works OK if the application has one upload button and 1 file to upload. However, in my application I have 3 different file upload buttons with 3 different files. Since the Java app window is not being closed, it is creating problem when second upload is happening. Basically, unable to upload second file…

    Kindly suggest any alternative. I have been struggling for 3 days….

    1. Hi Ravi,

      In this case you have to parameterized the script so that you can upload multiple files.

      1. Sharmila says:

        Hi Ravi, Were you able to resolve the issue on uploading multiple files? I am facing the same issue on the applet not getting closed.

  20. ravi550 says:

    Thanks Mukesh. The above code is working on Mac / Java / Selenium Web Driver. One typo is these 2 has to be different lines.

    //Copy to clipboard

    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);

    1. Hi Ravi,

      Thanks for suggestions. I will make the changes.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.