1

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?

Matt
  • 773
  • 2
  • 15
  • 30
  • Could you inspect the file upload button and post the HTML here? I've always been able to just `send_keys` to the hidden `` element that is usually present under the `Browse...` or `Upload..` buttons. – CEH Nov 01 '19 at 19:24
  • Hi Christine, I have added the code to the main question. I have been trying to use sendKeys to the 'file' element with id=manual_file_selection. This works fine all all browsers but Edge which won't allow me to use sendKeys – Matt Nov 01 '19 at 19:58
  • I added a solution that uses some JS that might help Edge recognize the input. I've never seen this issue with file upload working on all browsers but Edge, so this may or may not actually help you. – CEH Nov 01 '19 at 20:01

1 Answers1

2

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.

CEH
  • 5,701
  • 2
  • 16
  • 40
  • Christine you are a genius - it bloody worked :D Unfortunately I can't mark this as an answer as I don't have a high enough score. Thanks a lot for your help. – Matt Nov 02 '19 at 21:23