Automate File Upload window using Webdriver

6,625 views
Skip to first unread message

Denis Mager

unread,
May 4, 2012, 3:15:31 PM5/4/12
to webdriver
Hello.

Selenium RC worked before.
selenium.type("name=hidden-files", "C:\\ForUpload\\DSC_8225.jpg");

But now:

driver.findElement(By.name("hidden-files")).sendKeys("C:\\ForUpload\
\DSC_8225.jpg");

Doesn't work. Because link is not visible.

org.openqa.selenium.ElementNotVisibleException: Element is not
currently visible and so may not be interacted with

Mark Collin

unread,
May 8, 2012, 1:14:54 AM5/8/12
to webd...@googlegroups.com
You need to do what an end user would do to make the control visible before
interacting with it.

Also have a look at selenium.attachFile()
--
You received this message because you are subscribed to the Google Groups
"webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to
webdriver+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/webdriver?hl=en.


Denis Mager

unread,
May 8, 2012, 2:52:02 PM5/8/12
to webdriver
Yes. But user can choose file in pop-up Window. It window is not
Browser window.
How to upload file using pop-up "Windows" window?
I'll try to use only WebDriver.

On May 7, 10:14 pm, "Mark Collin" <mark.col...@lazeryattack.com>
wrote:

Mark Collin

unread,
May 8, 2012, 6:03:43 PM5/8/12
to webd...@googlegroups.com
You cannot interact with OS level dialogues using WebDriver.

Denis Mager

unread,
May 8, 2012, 6:21:09 PM5/8/12
to webdriver
Any idea how I can do it?

Bill Ross

unread,
May 8, 2012, 6:22:51 PM5/8/12
to webd...@googlegroups.com
On Windows, look into AutoIt.
I speculate there might be a workaround in using javascript to make
the input element visible.

Mark Collin

unread,
May 9, 2012, 9:21:55 AM5/9/12
to webd...@googlegroups.com
Either use sendKeys() or attachFile() and don't use the OS level dialogue,
or use something like AutoIT, or write a Java robot class that can interact
with OS level dialogues.

Denis Mager

unread,
May 15, 2012, 2:46:00 PM5/15/12
to webdriver
AutoIt working! Thanks!
1.Created script;
2. exported to .exe file;
3. Start .exe in Webdriver:
Runtime.getRuntime().exec("C:\\ForUpload\\PathAndEnter.exe");

But here is another problem. Script works under Win7, but doesn't
under Win2003 Server. :(
Any idea?

David

unread,
May 16, 2012, 3:04:15 AM5/16/12
to webdriver
What happens on win 2k3 where it fails?

Denis Mager

unread,
May 16, 2012, 1:57:47 PM5/16/12
to webdriver
On 2k3 different "File Upload" window. But by "AutoIt info Finder"
here is the same Window Title and Class of "Path field".
AutoIt script just stuck here...
On Win7 it OK.

Jim Evans

unread,
May 16, 2012, 2:06:18 PM5/16/12
to webd...@googlegroups.com
I don't have any answer for you here, but I think I'm going to bookmark this post so that I can point to it anytime anyone asks, "How do I automate file downloads[1] using WebDriver?" This is *exactly* why that particular feature doesn't exist in WebDriver.

--Jim

[1] I know you're trying to do a file *upload*, but the same principle applies.
> > > > > > webdriver+unsubscribe@googlegroups.com.
> > > > > > For more options, visit this group
> > > > > athttp://groups.google.com/group/webdriver?hl=en.
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google Groups
> > > > > "webdriver" group.
> > > > > To post to this group, send email to webd...@googlegroups.com.
> > > > > To unsubscribe from this group, send email to
> > > > > webdriver+unsubscribe@googlegroups.com.
> > > > > For more options, visit this group athttp://groups.google.com/group/webdriver?hl=en.
>
> > > > --
> > > > You received this message because you are subscribed to the Google Groups "webdriver" group.
> > > > To post to this group, send email to webd...@googlegroups.com.
> > > > To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

Panchdev Chauhan

unread,
May 16, 2012, 2:06:20 PM5/16/12
to webd...@googlegroups.com
java.awt.Robot class can be useful to achieve proper handling of upload file popup window(OS level interaction).
Regards,
 
PANCHDEV CHAUHAN
Software Engineer in Test
"Quality is the job I do."

@nuFuture Digital (India) Limited.
@Mobile: 88 66 268 514



Mike Riley

unread,
May 16, 2012, 4:36:37 PM5/16/12
to webd...@googlegroups.com
But that would only work if you were running locally and not through a remote server.  The same issue with AutoIt.  Neither can affect a system dialog on a remote system.

Mike

> > > > > > For more options, visit this group
> > > > > athttp://groups.google.com/group/webdriver?hl=en.
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google Groups
> > > > > "webdriver" group.
> > > > > To post to this group, send email to webd...@googlegroups.com.
> > > > > To unsubscribe from this group, send email to

> > > > > For more options, visit this group athttp://groups.google.com/group/webdriver?hl=en.
>
> > > > --
> > > > You received this message because you are subscribed to the Google Groups "webdriver" group.
> > > > To post to this group, send email to webd...@googlegroups.com.
> > > > To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

> > > > For more options, visit this group athttp://groups.google.com/group/webdriver?hl=en.

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

For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Mark Collin

unread,
May 17, 2012, 1:36:01 AM5/17/12
to webd...@googlegroups.com

Only if it had been a file download thread I’d have got ranty about how you probably don’t need to download the file in the first place ;)

 

One thing that I continue to see that I don’t really understand is the refusal of people to use the upload functions provided because they want to test the OS dialogue part, has anybody ever actually found a bug with the OS dialogue popup that has been valid doing these sort of tests?  Even if they did find such a bug would their developers be able to fix it?

 

From: webd...@googlegroups.com [mailto:webd...@googlegroups.com] On Behalf Of Jim Evans
Sent: 16 May 2012 19:06
To: webd...@googlegroups.com
Subject: [webdriver] Re: Automate File Upload window using Webdriver

 

I don't have any answer for you here, but I think I'm going to bookmark this post so that I can point to it anytime anyone asks, "How do I automate file downloads[1] using WebDriver?" This is *exactly* why that particular feature doesn't exist in WebDriver.

> > > > > > Doesn't work. Because link is not visible=

Anil Shahi

unread,
May 17, 2012, 9:44:43 AM5/17/12
to webd...@googlegroups.com

I have similar troubles, i handled the upload file ,coz it was in browser but download window handling was kindof impossible task for me on remote server. I m sure Auto It wont work on remote server neither it work on other Linux, Mac.
 
Here is what i found for download.
 
 
I m keen to know if anybody has proper solution for this issue.
 
Hey Mark,
 
Its not the OS dialogue people want to test, they want to test the files upload/Download to their respective applications, sometime it mght be the part of the dependent step in the testcase :).
 Thanks,
 
Anil
 
 
 
 

 

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

Mark Collin

unread,
May 17, 2012, 10:08:16 AM5/17/12
to webd...@googlegroups.com

Anil,

 

You say that people don’t want to test the OS dialogue, yet when given a solution that bypasses it most of them ignore it and go looking for complex workarounds like Java robot classes or AutoIT.  Selenium (Both the RC and WebDriver API’s) actually do a good job of dealing with file uploads.

 

File downloads are slightly more tricky and IMHO should be avoided unless absolutely necessary (i.e. you are going to actually do something with the file you download, don’t just download it to prove you can).  The code below does a good job (in my opinion, maybe not in others) of trying to work around the problems, but it’s not perfect and could do with a lot more work.

 

I have no problem with people performing upload/download tests, I just think that they should check that there is some worth in the test before blindly rushing ahead and trying to automate it.

Anil Shahi

unread,
May 17, 2012, 10:25:34 AM5/17/12
to webd...@googlegroups.com
 Mark,
 
I agree to your point that people blindly rush to automate file upload/download,   but I would prefer the solutions which bypasses it :)
Thanks for sharing your views.

Krishnan Mahadevan

unread,
May 17, 2012, 10:59:57 AM5/17/12
to webd...@googlegroups.com
I have had a problem with Uploading of Files especially when it involves RemoteWebDriver. Was wondering if anyone else has faced the same problem too ?

I vaguely remember the below code to be working, when I first created  a sample out of it, but now it doesnt. On the remote node, all I see is an error from the O.S which states, that the file location is invalid (which is obviously true, because on the remote machine, there isnt going to be the same directory structure as that which exists locally in my machine)

Here's a standalone implementation which doesnt seem to work for me with Selenium 2.21.0 (incase anyone wants to take a look at it)

public void uploadFile() throws InterruptedException {
myRemoteWebDriver.get("http://www.2shared.com/");
myRemoteWebDriver.setLogLevel(Level.ALL);
WebElement element = AbstractElement.locateElement("id=upField");
LocalFileDetector detector = new LocalFileDetector();
String path = "C:/Users/krmahadevan/Desktop/text.txt";
File f = detector.getLocalFile(path);
myRemoteWebDriver.setFileDetector(detector);
element.sendKeys(f.getAbsolutePath());
WebElement ele = AbstractElement.locateElement("//input[@title='Upload file']");
ele.click();
Thread.sleep(25000);
Alert a = myRemoteWebDriver.switchTo().alert();
System.out.println("Alert text was " + a.getText());
a.accept();
Thread.sleep(25000);
System.out.println("Page URL after upload = " + myRemoteWebDriver.getCurrentUrl());

}




Thanks & Regards
Krishnan Mahadevan

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

Mark Collin

unread,
May 17, 2012, 11:08:38 AM5/17/12
to webd...@googlegroups.com

It’s things like this that make me shy away from grid.

 

I would prefer to have a maven project checked out and run locally on the machine the tests should be run on.  You can then maven magic to get files from the resource directory and use them locally to run your tests.

Mike Riley

unread,
May 17, 2012, 12:23:31 PM5/17/12
to webd...@googlegroups.com
I solve the directory structure issue by running all my tests on a network mounted drive, which is the same letter on all systems.  So they are literally running in the same directory.  If you don't want to go that route, since it can be a performance issue, then simply have a duplicate directory structure on every test system.

Using the network mount also means I only have one place to update files and can easily look at the generated test reports from any system on that network, too.  It also means that my code running on system A has access to files created by my remote browser session on system B, although I have noticed Windows caching of network mounts can cause issues there in some cases.

using Grid allows me to leverage the different OS types and browser versions more easily, by having the discrete installations (on VMs in my case).

The only issue I have with Grid is that you can't do some things which require locality, like running AutoIT/Robot (as was discussed earlier), plus it also introduces it's own performance overhead compared to running locally.

Mike

To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.


For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.

To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.


For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.

To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.


For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.

To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.


For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.

To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

Krishnan Mahadevan

unread,
May 17, 2012, 12:34:16 PM5/17/12
to webd...@googlegroups.com
Mike, 

Unfortunately that is a route I cant take for a couple of reasons :

My framework gives the user an easy way of switching between my grid or the saucelabs cloud. So not sure how I can actually access a network share on my network from within a browser that is opened up via a VM spawned in the sauce labs cloud.

My Continuous Integration server runs out of a linux box under an account which has only UNIX credentials but no valid WINDOWS Corp credentials. So it becomes more difficult to have the files in one common place (And am really not keen on separating the test files from the test code which makes use of it, which means they all have to be checked into the version control system)

I was under the impression that the very reason that the class "org.openqa.selenium.remote.LocalFileDetector" was created was to resolve this because in my limited knowledge, I don't see any other practical usage for this class and for this method 
org.openqa.selenium.remote.RemoteWebDriver.setFileDetector(FileDetector)

if it cant work with Uploading of files, since I presume that upload can be easily achieved by using sendKeys() with all the other flavors of the WebDrivers (IEDriver, ChromeDriver, FFDriver)

or is it that am missing something which is pretty obvious here ?

Thanks & Regards
Krishnan Mahadevan

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



To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/GxOeBck7OeIJ.

To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.

Krishnan Mahadevan

unread,
May 17, 2012, 12:51:17 PM5/17/12
to webd...@googlegroups.com
Ignore my previous post folks. Silly me !!! 
It was a "User error" on my behalf. I wasn't doing it correctly.
So here's how I got it to work (in 2.21.0)

public static void main(String[] args) throws MalformedURLException, InterruptedException {
URL url = new URL("http://localhost:4444/wd/hub");
DesiredCapabilities dc = new DesiredCapabilities();
dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
RemoteWebDriver driver = new RemoteWebDriver(url, dc);
driver.setLogLevel(Level.ALL);
WebElement element = driver.findElement(By.name("upfile"));
LocalFileDetector detector = new LocalFileDetector();
String path = "C:/Users/krmahadevan/Desktop/text.txt";
File f = detector.getLocalFile(path);
                //the below line does the magic of ensuring that the file automatically gets shipped 
                //to the actual node.
((RemoteWebElement)element).setFileDetector(detector);
element.sendKeys(f.getAbsolutePath());
WebElement ele = driver.findElement(By.xpath("//input[@value='Press']"));
ele.click();
System.out.println("Page URL after upload = " + driver.getCurrentUrl());
System.out.println(driver.getPageSource());
driver.quit();
}


In this case, my hub and my IDE are running on 1 machine and my remote webdriver node that is attached to my local hub runs on a different machine (Just to be clear)

Here's what my webdriver remote node's console logs say (it confirms that the file did get uploaded, and so did my test's output as well, because the site that I used in my sample code, prints out the contents of the file that was uploaded)

22:16:07.223 INFO - Executing: org.openqa.selenium.remote.server.handler.UploadFile@18b1f8f at URL: /session/1337273151785/file)
22:16:07.223 INFO - Done: /session/1337273151785/file
22:16:07.489 INFO - Executing: [send keys: 0 org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement@fdfcb6d6, [C:\DOCUME~1\KRMAHA~1\LOCALS~1\Temp\1\1337273151785\upload3246960483036868825file\text.txt]] at URL: /session/1337273151785/element/0/value)
22:16:07.520 INFO - Done: /session/1337273151785/element/0/value



Thanks & Regards
Krishnan Mahadevan

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

Mike Riley

unread,
May 17, 2012, 12:53:44 PM5/17/12
to webd...@googlegroups.com
Not having used the Sauce Labs cloud I can't speak to that, but I would think that any system in the cloud could still mount a non-cloud shared directory.  You could use Samba on the Linux box to handle that or put Unix Services for Windows on a Windows box to handle an NFS mount from the Linux box.

I will have to check out that class you mentioned.  I have not looked at it previously.

Mike

Krishnan Mahadevan

unread,
May 17, 2012, 1:00:59 PM5/17/12
to webd...@googlegroups.com
Mike,

As I stated in my previous note, all of that wasnt even necessary :) RemoteWebElement came to my rescue :) 
All is well now, and I am now able to upload files using RemoteWebDriver when working with Grid and a remote node.

Thanks & Regards
Krishnan Mahadevan

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


To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/pxSS8VFO6oUJ.

To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.

Sumit Saxena

unread,
Jul 3, 2012, 8:26:52 AM7/3/12
to webd...@googlegroups.com
Anil Can you please post the code it will help lot of people

Thanks
Sumit

David

unread,
Jul 9, 2012, 1:51:37 PM7/9/12
to webd...@googlegroups.com
>> The only issue I have with Grid is that you can't do some things which require locality, like running AutoIT/Robot (as was discussed earlier), plus it also introduces it's own performance overhead compared to running locally. 

With exception to Grid using SauceLabs, there are ways to use Grid with AutoIt and Sikuli, not sure about Robot though. Here's a theoretical approach:


I haven't implemented this myself as haven't had the real need nor time for it yet, but will do so at some point. And I haven't heard of anyone else doing it. But it should be possible following my mentioned approach.
Message has been deleted

Ramesh P

unread,
Feb 29, 2016, 8:06:13 AM2/29/16
to webdriver
How to Upload file in selenium Webdriver by clicking on Flash button when browser is minimized?

File upload is working fine when browser is opened in main screen(Using Sikuli).  I want to upload files when browser is minimzed. How can it possible?



On Saturday, 5 May 2012 00:45:31 UTC+5:30, Denis Mager wrote:
Hello.

Selenium RC worked before.
selenium.type("name=hidden-files", "C:\\ForUpload\\DSC_8225.jpg");

But now:

driver.findElement(By.name("hidden-files")).sendKeys("C:\\ForUpload\
\DSC_8225.jpg");

David

unread,
Feb 29, 2016, 12:06:27 PM2/29/16
to webdriver
Ramesh, you're kind of hijacking an existing thread, you're better off posting a new thread than comment here. But in any case...

Why would anyone want to do a file upload on a minimized browser? Is that even possible from a manual test perspective - minimize browser then try file upload, how do you access the file upload UI when the browser is hidden from being minimized? Or did you mean start file upload by bringing up the file upload dialog but then minimize browser? But that shouldn't work since the file upload dialog is generally modal and blocking against other windows until you complete or dismiss it.
Reply all
Reply to author
Forward
0 new messages