Search code examples
seleniumselenium-webdrivermicrosoft-edge

Selenium how to upload files to Microsoft Edge


I am using the following code to upload files to a website to a 'file' type element.

The code works fine in Firefox, Chrome and Safari.

However when I run the code against Edge the file is NOT uploaded

driver.setFileDetector(new LocalFileDetector());
selectFile.sendKeys(path);

This error is reported: The command failed because the specified element is not pointer or keyboard interactable.

If I try using Javascript like this:

document.getElementById('manual_file_selection').sendKeys(path)

I get this: Object doesn't support property or method 'sendKeys'

As stated the same code works fine in Chrome, Firefox and Safari so I don't understand it.

This is the code behind the file upload button:

<div class="jsx-parser">
  <div data-xxxxx-element="manual-file-selection">
    <div class="button__container">
      <label for="manual_file_selection" class="button button--primary" data-dragging="false" data-xxxxx-element="manual-file-selection--label">
        <input id="manual_file_selection" type="file" accept="image/jpeg,image/png" data-xxxxx-element="manual-file-selection--input">
         <span>Select File</span>
      </label>
      </div>
 </div>
</div>

Anyone had any success uploading files to Edge with Selenium or is it not supported?


Solution

  • Based on your error messages, I'd give some Javascript a try. It's a bit hacky, as we execute JS to reveal the hidden input element, then send keys to it, but I've had success in the past.

    // fetch the element
    WebElement input = driver.findElement(By.XPath("//input[@type='file']"));
    
    // run JS to reveal the element
    JavascriptExecutor executor = (JavaScriptExecutor)driver;
    executor.executeScript("arguments[0].style.display = 'block';", input);
    
    // send file path keys
    input.sendKeys(path);
    

    It's worth a try. Let me know if this helps at all.