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
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.
SAMPLE Code to Upload file in Selenium in mac
//Click on the Import Button browser.findElement(By.className("importbutton")).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
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
Mukesh Otwani says
Hi Ashish, you can also use sendKeys method directly if you want to upload files.
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.
Mukesh Otwani says
Hi Ashish,
There are issues with Mac OS while using robot class.
Hamid says
Please separate the two lines below for avoiding confusion between comment and code:
//Copy to clipboard
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Mukesh Otwani says
Hi Hamid,
Thanks for pointing out the formatting issue 🙂
I’ll correct it
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.
Mukesh Otwani says
Hi Gokul, please share logs and other details to mukeshotwani@learn-automation.com
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.
Mukesh Otwani says
Hi Bhanu, please share your code to mukeshotwnai@learn-automation.com
Jenia says
Focusing on upload file window doesn’t work for me… using macOS Big Sur.
Do you have any suggestions?
Mukesh Otwani says
Hi Jenia, can u tell me what exactly the issue you are facing while working with robot class on MAC.
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
payal says
Can you please share same program with python instead of java
Mukesh Otwani says
Very soon…:)
VAIBHAV PRABHUGAONKAR says
WORKS LIKE A CHARM. THANK YOU MUKESH OTWANI
Mukesh Otwani says
Thanks, Vaibhav…:)
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.
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?
Mukesh Otwani says
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.
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!!!”
Mukesh Otwani says
Hi Purushotham,
It looks like known issue. Please refer this link https://bugs.openjdk.java.net/browse/JDK-8226806
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
Mukesh Otwani says
Hi Nikitha,
As of now there is no resolution for this issue
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
Mukesh Otwani says
Hi Shivam,
I need to check it.
Mithun says
Facing the same issue
Mukesh Otwani says
Hi Mithun,
From JDK bug link, it looks like Fix version has been mentioned as JDK 15
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 ?
Mukesh Otwani says
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
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
Mukesh Otwani says
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.
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
Mukesh Otwani says
Hi Avanti, What is the issue is coming now?
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.
Mukesh Otwani says
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.
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 ?
Mukesh Otwani says
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.
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.
Mukesh Otwani says
Hi Meet,
Thanks alot for your overwhelming comments. Your comments made my day…:)
Roman Glaz says
Great topic!!! thanks its works in my environment
Mukesh Otwani says
Hi Roman,
You’re always welcome…:). Feel free to ask your doubts on my blog.
Julius Glifonea says
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!
Mukesh Otwani says
Cheers Julius 🙂
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 ?
Mukesh Otwani says
Try with some other keyboard event using Robot class.
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….
Mukesh Otwani says
Hi Ravi,
In this case you have to parameterized the script so that you can upload multiple files.
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.
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);
Mukesh Otwani says
Hi Ravi,
Thanks for suggestions. I will make the changes.