How to simulate dragging and dropping a file from the file system with webdriver

5,388 views
Skip to first unread message

brianm709

unread,
Dec 2, 2011, 9:07:18 AM12/2/11
to Selenium Users
We are using the JQuery-File-Upload plugin (https://github.com/blueimp/
jQuery-File-Upload) in our application. One of the features is the
ability to drag a file from the file system and drop it onto the
component.

Is there a way to simulate dropping a file from the file system onto
an element using web driver?

Thanks

Danial Jaffry

unread,
Dec 2, 2011, 3:27:27 PM12/2/11
to seleniu...@googlegroups.com
My limited understanding of the web driver is that it gives you
capabilities that javascript within the browser would have. That being
said, javascript (at least within the browser) does not have access to
the client's file system or dragging of native OS elements. Javascript
can't even handle popup modals on browsers, as far as i know, because
they implemented differently based on OS.

I had a similar problem and ended up running a crude apple script that
did what I needed and was called via command line followed by a sleep.
Really hackety, but it worked, however it can't be run on any non OSX
OS.

> --
> You received this message because you are subscribed to the Google Groups "Selenium Users" group.
> To post to this group, send email to seleniu...@googlegroups.com.
> To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.
>

--
Danial Jaffry
(714) 312-6208

Daniel Wagner-Hall

unread,
Dec 4, 2011, 7:22:57 PM12/4/11
to Selenium Users
No, but I imagine their unit tests may be able to give you some
inspiration for how you might solve this problem

Joe

unread,
Dec 15, 2011, 4:31:38 PM12/15/11
to Selenium Users
I am looking for a solution to this also.
Did you find a solution?

On Dec 2, 2:07 pm, brianm709 <brianm...@gmail.com> wrote:

Nischand

unread,
Jun 26, 2012, 10:38:17 AM6/26/12
to seleniu...@googlegroups.com
Looking for a solution too... have seen some with Json with Ruby, not sure weather it works or not,
but need in java WD2,

can some body throw a light please,

Example:
http://s3u.github.com/har-view/   

in above URL i need to drop a file.

Thank you.

Mark Collin

unread,
Jun 26, 2012, 11:10:13 AM6/26/12
to seleniu...@googlegroups.com

No,

 

Selenium can’t interact with OS level dialogues.  To do something like this you would have to use something like a Java robot class, or a third party program (e.g. AutoIT for windows).

 

The only way to get it working using a pure selenium implementation would be to use the selenium file upload methods (assuming that this has not overridden the normal way to upload a file and made it impossible for Selenium to do this).

--

You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/6pZ_okHvwF4J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

vamsi chandra

unread,
Jun 26, 2012, 11:18:40 AM6/26/12
to seleniu...@googlegroups.com
Hi Mark,

yes i have seen AutoIT, it will handle window, but here i want to drop a file in to browser, i am not sure with AutoIT, and i have seen some post with Ruby - JSON, need to do in java ,
Thank q,
please have a look below url.

http://stackoverflow.com/questions/9797462/html5-drag-and-drop-using-selenium-webdriver-for-ruby

Mark Collin

unread,
Jun 26, 2012, 11:25:27 AM6/26/12
to seleniu...@googlegroups.com

If you need to do it in Java write a Java robot class as I said in my last message.  It will be hard to write, awkward to maintain and probably isn’t worth the effort in my opinion, but that’s how to do it if you really want to.

 

I would question why you need to specifically check the drag and drop functionality, but I suspect I’ll get an answer like “because we were told this had to be tested”.

vamsi chandra

unread,
Jun 26, 2012, 11:49:12 AM6/26/12
to seleniu...@googlegroups.com
No Mark, i have chosen that to Automate.
coz i need to drop HAR firebug reports in to my browser and have to validate.

Krishnan Mahadevan

unread,
Jun 26, 2012, 11:56:29 AM6/26/12
to seleniu...@googlegroups.com
Just curious. When you say you have validate a HAR report, what exactly are you referring to ?

I remember BrowserMob-Proxy (https://github.com/webmetrics/browsermob-proxy) having utilitarian methods that let you work with har files itself.



So wouldn't you be better off with using APIs that directly let you parse har files instead of going via the browser ?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Mark Collin

unread,
Jun 26, 2012, 11:57:58 AM6/26/12
to seleniu...@googlegroups.com

Why do you specifically *need* to drag and drop them?  Why can’t you pass an absolute path to the file in using .sendKeys()?

Krishnan Mahadevan

unread,
Jun 26, 2012, 12:07:59 PM6/26/12
to seleniu...@googlegroups.com
Mark, 
I believe that the html source of the URL that OP is referring to doesnt have any elements in it at all, but just an embedded javascript section in the <body> tag.

vamsi ,
To add on top of what I mentioned about browser mob proxy, I just realized that BMP might not have a way (or atleast I am not able to find one) wherein you can initialize it by referring to a file.

But you could use BMP for all of your har related activities also I guess.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


vamsi chandra

unread,
Jun 26, 2012, 12:27:25 PM6/26/12
to seleniu...@googlegroups.com
Hi Mahadevan,
i am just garbing my web-application response time and checking performance, to my previous results.
I have attached that HAR Report to know, what exactly i am validating.
Thank you.
Responce Time.JPG

vamsi chandra

unread,
Jun 26, 2012, 12:39:00 PM6/26/12
to seleniu...@googlegroups.com
Mark as Mahadevan told html source of the URL doesn't have any elements in it at all to use Send Keys, Actually HAR Reports can easily view only through some URL's and Fiddler2,

The one i am using for reports are: http://s3u.github.com/har-view/
so i need to drop, cant use send keys, even cant use AutoIT i believe, some work i need to do.

I am Attaching HAR Report, i have to-drop this file in to that browser, it will give proper result, i need to grab timings from that report.
Hope this makes clear, this is my idea actually. to do so.
HAR_Report.har

David

unread,
Jun 26, 2012, 1:41:05 PM6/26/12
to seleniu...@googlegroups.com
Just wanted to mention, if one were to automate this with AutoIt (or for more cross platform and possibly easier solution than AutoIt - ckeck out Sikuli), it would be similar to the low level APIs of WebDriver for doing a drag & drop:

first determine starting coordinate location of file to drag from the desktop
then determine ending coordinate location to drop file within browser
determine or add automation code to preconfigure test setup such that browser window is open and is top level window on desktop, and that a portion of desktop where file to drag & drop is exposed (i.e. browser window not maximized)

now issue sequence of commands:

move mouse to start location
mouse down on location/file on desktop (keep mouse down, if needed)
move mouse to end location
mouse up on location in browser

if the test required browsing for file via My Computer, Windows Explorer, etc. it will be a lot more complicated. Simpler to leave test file on the desktop for drag & drop.

Not sure if AutoIt can exactly handle that but possible, but definitely possible with Sikuli. And for AutoIt, you can also work with absolute vs relative coordinates on the screen. Relative means relative to a given window/object.

Be interesting if one could do a hybrid solution where you drag halfway from desktop to browser (via AutoIt/Sikuli/etc.) and then once it goes inside browser, WebDriver can find it and use WebDriver drag & drop to complete the process to drop where you want within browser. But I assume this isn't possible.

To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.


To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/6pZ_okHvwF4J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

Mark Collin

unread,
Jun 26, 2012, 3:29:20 PM6/26/12
to seleniu...@googlegroups.com

Having a quick look at it, it would seem that the way to handle this via Selenium would be to have a look at the JavaScript code and work out what happens when a window.ondrop is triggered.  I suspect it is somehow working out a reference to the local file location and using that to do everything else.  If so you just need to trigger the ondrop event with this reference.

 

The above is very simplistic and I suspect it’s not going to be as easy as I’ve made it sound above, but it must be said that this seems like a very convoluted way for you to create a report from your HAR file.  A quick google throws up:

 

http://www.frogthinker.org/projects/harlib

 

Which would enable you to read HAR files in as java objects and then you could plug the data into jgraph (or something similar).  I suspect something that did this may become very useful if you then open sourced it.

Reply all
Reply to author
Forward
0 new messages