File download dialog handling

1,925 views
Skip to first unread message

Prasanna

unread,
Nov 23, 2009, 9:43:22 PM11/23/09
to webdriver
At the height of being repetitive, is there a way to handle file
download dialogs using webdriver? I do see Issue 27,
http://code.google.com/p/webdriver/issues/detail?id=27, and tried the
javascript hacks but to no avail.

Basically this is what happens, I have a button which when clicked
will display the file download prompt with two radio buttons, "Open
with" and "Save File". By default "Open With" is selected. Then there
are 'Ok' and 'Cancel ' buttons. The first thing I would like to do is
when the download prompt comes up, click Ok, so that the file is
opened.

For this I tried:

((JavascriptExecutor) driver).executeScript("window.confirm = function
(msg) { return
true; }");

before clicking the button to download. Did not work.

The next I want to select the 'Save File' radio button and then Click
'Ok'.

Could someone please let know some workarounds. Thanks

Simon Stewart

unread,
Nov 24, 2009, 3:59:48 AM11/24/09
to webd...@googlegroups.com
No, there's currently no way to handle the "save file" dialog. I won't
be working on this feature until we've got native events for all
platforms, can handle javascript alerts and prompts cleanly, have the
Chrome driver stable and can access sites with insecure SSL certs.

I am, however, happy to receive, review and apply patches, so if
you're willing to work on the feature, it'll be in webdriver sooner.

Simon

Eran M.

unread,
Nov 24, 2009, 5:53:20 AM11/24/09
to webd...@googlegroups.com
Hi,

If your purpose is to save the file, it may be possible to extend the extension to handle the MIME type you are trying to open and get it to be automatically downloaded. I'm updating the issue with some links to related Mozilla documents.
What point is there in opening the file for you? Any program which will be opened cannot (and will not) be controlled using WebDriver, unless it's a script that's processing the file.

Eran

2009/11/24 Simon Stewart <simon.m...@gmail.com>

Prasanna

unread,
Nov 24, 2009, 1:31:31 PM11/24/09
to webd...@googlegroups.com
I can try that. If you can point me to docs/wiki, that would be nice.
--
- Prasanna

Eran M.

unread,
Nov 24, 2009, 2:06:04 PM11/24/09
to webd...@googlegroups.com
See issue 95
Mozilla Developers Center is not the most organized site in existence, but once you find out the exact component you should implement, it's probably just a matter of making sure that file is being handed to the download manager.

Eran

2009/11/24 Prasanna <prasa...@gmail.com>

Penelope

unread,
Nov 25, 2009, 8:29:40 AM11/25/09
to webdriver
As a matter of fact, I have just done this. What I found to be the
easiest method is to create a profile that you will only use for your
Webdriver testing. Start Firefox with that profile (firefox -P
<profilename>) and set up all your preferences, such as saving certain
MIME types. This is most easily done by doing exactly what you will do
in your test and then clicking "Save" and "Always do this." That will
add that MIME type to your list of file types that is handled
automatically in Firefox. You can confirm it saved your action by
going into Tools | Options and then clicking on Applications. You
should be able to find the MIME type you just dealt with and confirm
that it will Save as the default action.

Then, in your webdriver code, tell it to use this same profile by
setting the property webdriver.firefox.profile, and you're good to go.

The one caveat to this is that if your file is an executable, Firefox
will not enable the "Always do this" checkbox. At that point, you need
to edit one of the files in the profile.

This is kind of out of scope for Webdriver, but I've spent a lot of
time on it so I thought I'd share. :)

cheers,
Penelope

On Nov 24, 2:06 pm, "Eran M." <eran....@gmail.com> wrote:
> See issue 95 <http://code.google.com/p/webdriver/issues/detail?=95>.
> Mozilla Developers Center is not the most organized site in existence, but
> once you find out the exact component you should implement, it's probably
> just a matter of making sure that file is being handed to the download
> manager.
>
> Eran
>
> 2009/11/24 Prasanna <prasanna...@gmail.com>
>
>
>
> > I can try that. If you can point me to docs/wiki, that would be nice.
>
> > On Tue, Nov 24, 2009 at 2:53 AM, Eran M. <eran....@gmail.com> wrote:
>
> >> Hi,
>
> >> If your purpose is to save the file, it may be possible to extend the
> >> extension to handle the MIME type you are trying to open and get it to be
> >> automatically downloaded. I'm updating the issue with some links to related
> >> Mozilla documents.
> >> What point is there in opening the file for you? Any program which will be
> >> opened cannot (and will not) be controlled using WebDriver, unless it's a
> >> script that's processing the file.
>
> >> Eran
>
> >> 2009/11/24 Simon Stewart <simon.m.stew...@gmail.com>
>
> >>> No, there's currently no way to handle the "save file" dialog. I won't
> >>> be working on this feature until we've got native events for all
> >>> platforms, can handle javascript alerts and prompts cleanly, have the
> >>> Chrome driver stable and can access sites with insecure SSL certs.
>
> >>> I am, however, happy to receive, review and apply patches, so if
> >>> you're willing to work on the feature, it'll be in webdriver sooner.
>
> >>> Simon
>
> >>> On Tue, Nov 24, 2009 at 2:43 AM, Prasanna <prasanna...@gmail.com> wrote:
>
> >>> > At the height of being repetitive, is there a way to handle file
> >>> > download dialogs using webdriver? I do see Issue 27,
> >>> >http://code.google.com/p/webdriver/issues/detail?id=27, and tried the
> >>> > javascript hacks but to no avail.
>
> >>> > Basically this is what happens, I have a button which when clicked
> >>> > will display the file download prompt with two radio buttons, "Open
> >>> > with" and "Save File". By default "Open With" is selected. Then there
> >>> > are 'Ok' and 'Cancel ' buttons. The first thing I would like to do is
> >>> > when the download prompt comes up, click Ok, so that the file is
> >>> > opened.
>
> >>> > For this I tried:
>
> >>> > ((JavascriptExecutor) driver).executeScript("window.confirm = function
> >>> > (msg) { return
> >>> > true; }");
>
> >>> > before clicking the button to download. Did not work.
>
> >>> > The next I want to select the 'Save File' radio button and then Click
> >>> > 'Ok'.
>
> >>> > Could someone please let know some workarounds. Thanks
>
> > --
> > - Prasanna- Hide quoted text -
>
> - Show quoted text -

Prasanna

unread,
Nov 25, 2009, 1:21:06 PM11/25/09
to webd...@googlegroups.com
This is the workaround I tried using HtmlUnitDriver, but I am not sure whether it is a bug.

HtmlUnitDriver htmlUnitdriver = new HtmlUnitDriver();
WebElement element = htmlUnitdriver.findElement(By.id("download_file")).click();
String fileContents = element.getPageSource();

The fileContents contain the contents of the file that I wish to download. Since getPageSourcfe() is going to be of format text/plain, any file that has only textual contents should work with this method. I am not sure about dowloading image files or files that contain both text and images.

But the problem is the context of the page on which the download link was present is lost.
--
- Prasanna

Prasanna

unread,
Nov 25, 2009, 1:23:28 PM11/25/09
to webd...@googlegroups.com
Sorry, wrong code snippet pasted in my previous mail

HtmlUnitDriver htmlUnitdriver = new HtmlUnitDriver();
htmlUnitdriver.findElement(By.id("download_file")).click();
String fileContents = htmlUnitdriver.getPageSource();
--
- Prasanna

Eran M.

unread,
Nov 25, 2009, 6:14:42 PM11/25/09
to webd...@googlegroups.com
Of course, you could always do driver.navigate().back().

Eran

2009/11/25 Prasanna <prasa...@gmail.com>
Reply all
Reply to author
Forward
0 new messages