How to upload file in headless mode

Hello,
I’m trying to run file upload in headless mode, I have tried different ways, but not work. How can I get file upload in headless mode?

  1. The import button in DOM is like below:

    It is not input tag, I’m not sure if this will impact on the result ?
  2. I need it to run in headless mode.

Following are all the ways I have tried:

  1. Using WebUI.uploadFile keyword
    Not work in default mode and headless mode.

  2. Define custom Keywords like below:
    public class MyTools {
    @Keyword
    def uploadFile (TestObject to, String filePath) {
    WebUI.click(to)
    WebUI.delay(2)
    StringSelection ss = new StringSelection(filePath);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

     Robot robot = new Robot();
     robot.keyPress(KeyEvent.VK_CONTROL);
     robot.delay(1000)
     robot.keyPress(KeyEvent.VK_V);
     robot.keyRelease(KeyEvent.VK_V);
     robot.delay(1000) //NOTE THE DELAY (500, 1000, 1500 MIGHT WORK FOR YOU)
     robot.keyRelease(KeyEvent.VK_CONTROL);
     robot.keyPress(KeyEvent.VK_ENTER);
     robot.keyRelease(KeyEvent.VK_ENTER);
    

    }
    }
    This way can run in default Chrome mode, but will always fail in headless mode.

  3. Use following code:
    WebDriver driver = DriverFactory.getWebDriver()
    String path = ‘D:\Daily task\New PW User.csv’
    driver.findElement(By.xpath(“//*[@id=‘app’]/div/section/div/section[1]/div[1]/div/button”)).sendKeys(path);
    Not work both in default mode and headless mode.

Can someone help me on this?

1.) There should be no difference in the effectiveness of headless testing versus your normal browser testing. Is the upload working when you try any of your methods with a non-headless run?

2.) Are you sure that the only element involved in the upload is the <button> element you’ve shown us? This is highly unlikely (usually it would be an <input> element that would recieve the path to the file you are trying to upload). Can you share a screenshot of the GUI when you are uploading the file? My guess is that you hit the button, then you should get another field to actually receive the upload path.

Hello,
I have the same problem, did you find the solution?

is it because Robot doesn’t work in headless mode?

Same issue here. Haven’t found a way to upload files in headless mode.

That may have some sense.
Since headless mode it is not aware of any physical / virtual display, therefore no ‘window’ is present, I don’t see how such should work.
Robot (not Robot framework) is a lib to interract with native input libs.

From the API docs:

Note that some platforms require special privileges or extensions to access low-level input control. If the current platform configuration does not allow input control, an AWTException will be thrown when trying to construct Robot objects. For example, X-Window systems will throw the exception if the XTEST 2.2 standard extension is not supported (or not enabled) by the X server.