Getting HTMLUnit to download files

4,188 views
Skip to first unread message

Chris Osborn

unread,
Dec 31, 2011, 6:36:42 PM12/31/11
to seleniu...@googlegroups.com
Hello all,

I need to download files from a website using Selenium (via Python bindings)
This works fine in Chrome: I perform a "click" on the appropriate element and Chrome downloads the file into the Downloads directory.

However, I need to do the same in HTMLUnit.
Currently the "click" is not triggering a download.

Is there a way to get it to do so?


Thanks in advance,
Chris

Mark Collin

unread,
Jan 1, 2012, 4:28:32 AM1/1/12
to seleniu...@googlegroups.com

Here’s my implementation of a file downloader:

 

https://github.com/Ardesco/Ebselen/blob/master/ebselen-core/src/main/java/com/lazerycode/ebselen/customhandlers/FileDownloader.java

 

It uses selenium to get the download link and then downloads it through java using the apache commons library.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/F88SWHDAYloJ.
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.


-- This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited. If you have received this email in error please notify postm...@ardescosolutions.com

Chris Osborn

unread,
Jan 1, 2012, 4:19:16 PM1/1/12
to seleniu...@googlegroups.com
Thanks Mark!
This code looks very nice and I may be able to extract a working solution from it.


However, I am using Python and am connecting to the "selenium server" Java application through Python bindings.

What I would really like to be able to do is, through the Python bindings if possible:

  tell the selenium Java server to tell HTMLUnit to download files to a certain location, instead of discarding them.

That way the download can triggered by a button click, as usual, and I do not need to manually extract the state of the browser session,
or write any Java code.

Any suggestions?


Thanks much,
Chris

Mark Collin

unread,
Jan 3, 2012, 6:36:54 AM1/3/12
to seleniu...@googlegroups.com

The usual ones are use something like auto it (Windows only) to click OK on any download dialogue that appears, or write a Java Robot (Or your own language equivalent) to interact with OS level dialogues.

 

Another option would be to convert what I have written into Python, I’m sure there must be some libraries similar to apache commons somewhere to make life easy, lets face it Python is a pretty big deal J

--

You received this message because you are subscribed to the Google Groups "Selenium Users" group.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/AvIgNCoRjE4J.


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.

Chris Osborn

unread,
Jan 6, 2012, 6:31:57 PM1/6/12
to seleniu...@googlegroups.com
Thanks again, Mark.

I think I will end up trying to convert your Java code to Python.
In my case, an explicit download via. code such as you provided is not the most desirable solution, because it cannot be triggered by javascript code/button presses. 
However, it sounds like it is the best available compromise, and it will likely do everything I need.

(I think that in order to trigger your code appropriately in response to javascript code/button presses, I would have to modify the selenium server itself.  This code is in a separate
process and I don't have the source for it, plus I'd rather not touch it.  Also, the robot solutions will not work in my case, because I am on Linux and I don't believe HTMLUnit puts up dialog boxes;
it merely refuses to place the downloads anywhere.  Please let me know if I have missed anything here.)


Thanks again,
Chris












Mark Collin

unread,
Jan 9, 2012, 6:24:24 AM1/9/12
to seleniu...@googlegroups.com

The only other thing I can think of is setting HTMLUnit up to auto download to a set location when a link is clicked (Like you can with FireFox).  However I have no idea if HTMLUnit is capable of doing this or not.

 

If not you are probably looking at tweaking the HTMLUnit code which would then involve you pulling in your tweaked version of HTMLUnit into Selenium which means building a custom Selenium and using that in your tests.  Would be a lot of work, however it would probably be useful for somebody else if committed back.

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Chris Osborn
Sent: 06 January 2012 23:32
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Getting HTMLUnit to download files

 

Thanks again, Mark.

--

You received this message because you are subscribed to the Google Groups "Selenium Users" group.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/tLhjs1aBb0EJ.


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.

Chris Osborn

unread,
Jan 10, 2012, 4:15:09 PM1/10/12
to seleniu...@googlegroups.com
Yeah, it seems to come down to having to write code in both HTMLUnit and the Selenium server.
Alas, for my current project, I do not have the resources to do that work.

I ended up writing code similar to what you provided, and it's working just fine.


Thanks again,
Chris
Reply all
Reply to author
Forward
0 new messages