Selenium for uploading a file on a Mac device

I am facing a challenge when trying to upload a file on my MAC using selenium with Java. The button I need to interact with does not have an input tag, so I attempted to use robot script and even apple script but haven't been successful. Is there anyone who can assist me?

Apple script

    tell application "System Events"
    --one second delay
    delay 1
    --To press command+shift+G
    keystroke "G" using {command down, shift down}
    delay 2
    --Enter file path
    keystroke "file path"
    
    keystroke return
    delay 1
    keystroke return
    
end tell

Java code :

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(filePath);

Additionally, I would like to know how to pass the filePath from Java to the apple script.

Answer №1

After experimenting with various approaches, I found the following code to be effective on my MAC without any issues. I incorporated "Command + W" keys into the solution.

//Click on the Choose file Button
browser.findElement(By.className("choosefile")).click();
//Path of file need to be upload
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);
try {
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_META);
    robot.keyPress(KeyEvent.VK_W);
    robot.keyRelease(KeyEvent.VK_META);
    robot.keyRelease(KeyEvent.VK_W);
    robot.keyPress(KeyEvent.VK_META);
    robot.keyPress(KeyEvent.VK_SHIFT);
    robot.keyPress(KeyEvent.VK_G);
    robot.keyRelease(KeyEvent.VK_G);
    robot.keyRelease(KeyEvent.VK_SHIFT);
    robot.keyRelease(KeyEvent.VK_META);

    // Paste the clipBoard content - Command ⌘ + V.
    robot.keyPress(KeyEvent.VK_META);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_META);

    // Press Enter (GO - To bring up the file.)
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    robot.delay(1000 * 4);

    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

} catch (AWTException e) {
    e.printStackTrace();
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having difficulty providing input (Date) in a web application using Selenium Webdriver with Python

I'm having difficulty inputting a date into a datefield on a web application using Selenium Webdriver with Python. I have attempted the following code: driver.find_elements_by_name("invDate")[0].click() This code successfully places the cursor in th ...

Jackson can convert property fields starting with "property_" into a list during deserialization

My task involves serializing an object class: public class DigitalInput { private String id; private Date timestamp; private String matter; private String comment; private String channelId; private List<IndexableProperty> o ...

Exploring the capabilities of Selenium 4 for adjusting column sizes in C#

In my website testing, I have been using Selenium 3.141 and now want to upgrade to the latest version, Selenium 4.4. However, I am facing issues with resizing columns in a table due to changes in Selenium Actions. Below is the code that worked for me in S ...

Managing Windows Authorization using Selenium WebDriver and Internet Explorer 10 - A Step-by-Step Guide

I am experiencing difficulties with Windows authentication while trying to create an automation test (in C#) using Selenium Webdriver with the InternetExplorer Driver. Although I can access https//username:[email protected] successfully through Firef ...

Using Python with Selenium: Changing the attribute from "disabled" to "enabled" in web automation

I have been using the following code: commit = driver.find_element(by=By.XPATH, value="/html/body/div[2]/div/div/div/form/div[1]/div[4]/button") driver.execute_script("arguments[0].setAttribute('enabled', true)", commit) Unfortunately, the attri ...

Using jQuery to send a post request to a servlet and then refreshing the parent

As I dive into the world of ajax jQuery documentation, my goal is to send a post call to a servlet that I have created in order to save an entry in my database. Once the post is successful, I aim to reload the parent of the iFrame responsible for rendering ...

The functionality of Selenium is not supported on Firefox 57 when using php

After installing Selenium Server Standalone 3.9.1, XAMPP 3.2.2, php-webdriver 0.9.1, and Firefox 57, I attempted to execute the example.php file located in the root of the selenium folder. The code snippet is as follows: require_once "phpwebdriver/WebDriv ...

Unable to retrieve the element from the website using Selenium

Having trouble accessing the element to change pages one by one. Ongoing attempts have been unsuccessful. Reference image: http://prntscr.com/o0f4mx. Would greatly appreciate any assistance. XPath = //*[@id="___gcse_0"]/div/div/div/div[5]/div[2]/div/div/d ...

Preventing Cross-Site Request Forgery in File Upload Submissions

Within my AngularJS application, I have successfully implemented Angular's CSRF protection mechanism for all POST, PUT, and other nonsafe web service calls. However, I encountered a challenge with one specific scenario: performing a multipart/form-dat ...

Waiting for Capybara to wait for the AJAX to finish loading

Hello, I am experiencing an issue with my capybara testing environment. Error: jQuery is not defined (Session info: chrome=43.0.2357.125) I suspect that this problem may be related to the ajax wait function. def wait_for_ajax Timeout.timeou ...

From Android JSON array to a list of items

In my android app, I have encountered an issue while trying to parse a JSONArray into an ArrayList. The PHP script returns the expected results correctly, but when attempting to add the results to the ArrayList in Java, a null pointer exception occurs at ...

Tips for sharing a single driver instance in the Page Object Model

My automation framework utilizes selenium, TestNG, and the PageObject model. In terms of structure: https://i.stack.imgur.com/xLPYB.png For my Testng class / test case: https://i.stack.imgur.com/WFM3p.png I encountered a null pointer error https://i.s ...

Locating elements in Selenium using Python by matching only a portion of its id

I am currently working with Selenium in Python and I need to locate an element using only part of its id name. What would be the best approach for this? For instance, let's say I have already located an item with the id name coption5 like so: sixth_ ...

What is the proper way to identify this element in Selenium?

After crafting this piece of code driver.findElement(By.xpath("//input[@type='textarea']")).sendKeys("foo"); This was the outcome BrowserController.remoteControl(): Unable to locate element: //input[@type='textarea'] https://i.stack ...

When sending an HTTP POST request to a Nodejs service with an uploaded file, the request fails after 8-15 seconds on Firefox and 25 seconds on Chrome

My web app is built on Angular 7. I am facing an issue while trying to send larger files to a Node.js service. Smaller files, around 3mb, are being sent successfully but when attempting to send bigger files like 20mb, the request gets cut off. In Chrome, I ...

Is there a way to upload files in AngularJS without using AJAX or jQuery?

Currently, I am looking to create a gallery that allows for the uploading of multiple images. While I have come across some options that utilize ajax to immediately send the files, I prefer a solution that involves form submission. In addition, I would li ...

Require remote access to a Firefox browser on a connected machine through an open connection in a robot test case

Issue at hand: Attempting to run a Robothost Testcase which involves using the Open Connection keyword to connect to machineX and then utilizing the Open Browser keyword to open Firefox on machineX. However, instead of opening the Firefox browser on machin ...

What is the best way to transmit a JSON object to REST services using Angular?

Whenever I attempt to send the JSON object to REST services, I encounter an error that looks like this: http://localhost:8080/api/v1/cardLimit 400 (Bad Request); JSON Object Example: public class GameLimit implements Serializable { private stati ...

javax.el.MethodNotFoundException: JSP method cannot be located

While compiling the JSP template, I encountered the following exception: The method getFriendImage() is not recognized I attempted to rebuild the class without success. The src attribute contains a valid link to an image that is functioning properly. ...

Learn the process of dynamically extracting a targeted cell value from a table with Selenium and Python

Currently, I am developing an automation script using Selenium and Python with the following tasks: The script needs to dynamically read the rows and columns of a table. It should specifically search for a column (which remains constant) that contains a ...