0

how can I simulate the action of dragging and dropping a file from the filesystem to an element that has a ondrag event trigger?

As for the normale "file" input, I was able to set the value of the input with jQuery. Can't I create a javascript File object or use any similar hack?

Thanks

Thanks

4 Answers 4

3

There is a commercial wrapper around Selenium called Helium that lets you do what you want in one command:

drag_file('/path/to/file.txt', to=driver.find_element_by_id("yourElement"))

(I am affiliated with Helium.)

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like it's a matter of either sending the filepath to the input, because I think that's all dragging the file around really does, or use a created element with JS and use drag_and_drop_by_offset with move to element.

Comments

1

Selenium only works with your web browser. If you are opening something other than a web browser such as file browser you cannot interact with it. Drag and drops work with items within a web browser but not from program such as Windows Explorer or a Linux file explorer to a web browser. Create and element in your browser with jQuery and drag and drop it.

1 Comment

Hi, what object can I create that generates an event with a files that can be used by FileReader.readAsDataURL? This is what dragging a file from the filesystem does and also what I do need. Thanks
1

You might want to try something like this

query = '''function previewFile() {
          var preview = document.querySelector('img');
          var file    = document.querySelector('input[type=file]').files[0];
          var reader  = new FileReader();

          reader.onloadend = function () {
            preview.src = reader.result;
          }

          if (file) {
            reader.readAsDataURL(file);
          } else {
            preview.src = "";
          }
        }'''

driver.execute_script(query)

You will have to play around with the details but this should read your file and you may have to send it to somewhere in your DOM.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.