• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / How to upload files in Selenium Webdriver in MAC OS

How to upload files in Selenium Webdriver in MAC OS

August 6, 2015 by Mukesh Otwani 51 Comments

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

Filed Under: Advance Selenium Tagged With: File Upload in MAC OS

Reader Interactions

Comments

  1. Ashish says

    September 26, 2022 at 7:20 PM

    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

    Reply
    • Mukesh Otwani says

      October 8, 2022 at 1:14 PM

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

      Reply
      • Ashish says

        October 31, 2022 at 2:17 PM

        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.

        Reply
        • Mukesh Otwani says

          November 16, 2022 at 9:36 PM

          Hi Ashish,

          There are issues with Mac OS while using robot class.

          Reply
  2. Hamid says

    August 16, 2022 at 6:58 PM

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

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

    Reply
    • Mukesh Otwani says

      August 26, 2022 at 10:21 PM

      Hi Hamid,

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

      Reply
  3. Gokul says

    June 29, 2022 at 1:19 PM

    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.

    Reply
    • Mukesh Otwani says

      July 22, 2022 at 1:11 PM

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

      Reply
  4. Bhanu Chaitanya says

    February 23, 2022 at 3:46 PM

    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.

    Reply
    • Mukesh Otwani says

      March 9, 2022 at 9:50 AM

      Hi Bhanu, please share your code to mukeshotwnai@learn-automation.com

      Reply
  5. Jenia says

    September 1, 2021 at 5:06 PM

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

    Reply
    • Mukesh Otwani says

      September 4, 2021 at 1:44 PM

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

      Reply
    • Olu says

      September 15, 2021 at 4:28 PM

      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

      Reply
  6. payal says

    August 26, 2021 at 12:57 PM

    Can you please share same program with python instead of java

    Reply
    • Mukesh Otwani says

      August 27, 2021 at 1:36 PM

      Very soon…:)

      Reply
  7. VAIBHAV PRABHUGAONKAR says

    June 7, 2021 at 5:54 PM

    WORKS LIKE A CHARM. THANK YOU MUKESH OTWANI

    Reply
    • Mukesh Otwani says

      June 9, 2021 at 9:22 PM

      Thanks, Vaibhav…:)

      Reply
  8. Mridul Chawla says

    July 23, 2020 at 3:12 PM

    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.

    Reply
  9. Samreen Adil says

    February 4, 2020 at 11:39 PM

    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?

    Reply
    • Mukesh Otwani says

      February 7, 2020 at 2:44 PM

      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.

      Reply
  10. purushotham says

    September 25, 2019 at 2:04 PM

    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!!!”

    Reply
    • Mukesh Otwani says

      September 25, 2019 at 5:07 PM

      Hi Purushotham,

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

      Reply
    • Nikitha says

      October 10, 2019 at 2:52 PM

      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

      Reply
      • Mukesh Otwani says

        October 10, 2019 at 5:06 PM

        Hi Nikitha,

        As of now there is no resolution for this issue

        Reply
        • Shivam Gupta says

          May 12, 2020 at 12:46 PM

          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

          Reply
          • Mukesh Otwani says

            May 12, 2020 at 6:55 PM

            Hi Shivam,

            I need to check it.

    • Mithun says

      July 20, 2020 at 7:17 PM

      Facing the same issue

      Reply
      • Mukesh Otwani says

        July 20, 2020 at 9:40 PM

        Hi Mithun,

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

        Reply
  11. Navi says

    June 29, 2019 at 9:34 PM

    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 ?

    Reply
    • Mukesh Otwani says

      June 29, 2019 at 11:39 PM

      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

      Reply
  12. Hardy says

    May 22, 2019 at 4:07 PM

    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

    Reply
    • Mukesh Otwani says

      May 22, 2019 at 7:17 PM

      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.

      Reply
  13. Avanti says

    April 23, 2019 at 12:13 AM

    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

    Reply
    • Mukesh Otwani says

      April 23, 2019 at 12:26 AM

      Hi Avanti, What is the issue is coming now?

      Reply
      • Avanti says

        April 25, 2019 at 3:20 PM

        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.

        Reply
        • Mukesh Otwani says

          April 25, 2019 at 3:51 PM

          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.

          Reply
  14. Vishal Roy says

    April 5, 2019 at 4:55 PM

    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 ?

    Reply
    • Mukesh Otwani says

      April 5, 2019 at 8:00 PM

      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.

      Reply
  15. Meet says

    March 28, 2019 at 12:14 PM

    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.

    Reply
    • Mukesh Otwani says

      March 28, 2019 at 1:23 PM

      Hi Meet,

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

      Reply
  16. Roman Glaz says

    March 25, 2019 at 7:27 PM

    Great topic!!! thanks its works in my environment

    Reply
    • Mukesh Otwani says

      March 25, 2019 at 9:41 PM

      Hi Roman,

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

      Reply
  17. Julius Glifonea says

    February 15, 2017 at 11:08 AM

    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!

    Reply
    • Mukesh Otwani says

      February 16, 2017 at 11:39 AM

      Cheers Julius 🙂

      Reply
  18. Nishchay Gupta says

    July 13, 2016 at 1:48 PM

    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 ?

    Reply
    • Mukesh Otwani says

      July 14, 2016 at 11:11 AM

      Try with some other keyboard event using Robot class.

      Reply
  19. ravi550 says

    February 24, 2016 at 3:40 AM

    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….

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 2:19 PM

      Hi Ravi,

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

      Reply
      • Sharmila says

        April 19, 2016 at 3:11 AM

        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.

        Reply
  20. ravi550 says

    February 17, 2016 at 12:53 AM

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

    Reply
    • Mukesh Otwani says

      February 22, 2016 at 5:16 PM

      Hi Ravi,

      Thanks for suggestions. I will make the changes.

      Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • Syllabus For Playwright Online Training Program
  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?