views:

1395

answers:

4

i'm trying to save an image from a website using selenium server & python client. i know the image's URL, but i can't find the code to save it, either when it's the the document itself, or when it's embedded in the current browser session.

the workaround i found so far is to save the page's screenshot (there are 2 selenium methods for doing just that), but i want the original image.

i don't mind fiddling with the clicking menu options etc. but i couldn't found how.

thanks

A: 

How about going to the image URL and then taking a screenshot of the page? Firefox displays the image in full screen. Hope this helps..

Webber
as i wrote already, this is my current workaround.
Berry Tsakala
A: 

I haven't used selenium, but if you know the image's URL, why not just do:

from urllib import urlretrieve

urlretrieve(url, filename)

which will save the url to the filename. more info here

Adrian Mester
because saving the image depends on the session. the reason for selenium in the first place is testing on a real environment.the server gets the url indeed, but it parses lots of environment variables related to the session before deciding which image to deliver, if any!
Berry Tsakala
then, maybe you can get the cookies from selenium and use urllib2 to also pass them in the request?
Adrian Mester
session is more than just cookies, and to mimic it in urrlib2, means to mimic Selenium RC, so why would i use selenium in the first place??
Berry Tsakala
+2  A: 

To do this the way you want (to actually capture the content sent down to the browser) you'd need to modify Selenium RC's proxy code (see ProxyHandler.java) and store the files locally on the disk in parallel to sending the response back to the browser.

Patrick Lightbody
That's ... interesting. But then it will save ALL files, and i'll have to guess which file belongs to which point in time, not to mention learning a bit of Java. But it's a possible solution.
Berry Tsakala
+1  A: 

Hello,

I was trying to accomplish the same task, but the images I wanted to grab were the size of my monitor (wallpaper) -- so the capture screenshot workaround didn't work for me. I figured out a way to do it...

I've got selenium set up to go to the page I want (which induces all the session goodies) Then I used a program called "Workspace Macro" to loop through the selenium tasks.

Grab it from here http://www.tethyssolutions.com/product.htm -- they have a trial version, which I think works for 30 runs or something.

So here's the progression:

  • start firefox
  • open selenium and load test case
  • start it, but quickly pause it.
  • record a macro, which pushes "step" on selenium, then goes over to the firefox window and clicks file->save page as, saves, then stop recording
  • run the macro x times...
  • profit??

Cheers

HumanUmbrella