Save images from a website (with watir)
Asked Answered
H

3

7

How can i save website images with watir, without reloading them with open-uri or similar?

I: The reason why i can't use

      File.open(file_name, 'wb') do |f|
             f.write open(img.src).read
      end # file open

is that the images are generated in the current (login-)session and only once, so an "external" 2nd access isn't possible.

II: browser.images.save() - only for ie - isn't helpful either, it opens the save-to dialogue. So its so useless for automation.

Examples: http://wiki.openqa.org/display/WTR/Save+All+Images+on+a+Webpage

 require 'watir'
 browser = Watir::Browser.new :ie
 browser.goto 'http://google.com'

 idx = 0
 browser.images.each do |x|
   puts idx
   idx += 1
   location = 'c:\tmp\file-' + idx.to_s + '.jpg'
   x.save(location)
 end

github source: http://rubydoc.info/github/watir/watir-classic/Watir/Image

    # File 'lib/watir-classic/image.rb', line 48

    def save(path)
    @container.goto(src)
     begin
      fill_save_image_dialog(path)
      @container.document.execCommand("SaveAs")
     ensure
      @container.back
     end
    end

My best idea atm is to fetch all images by using a proxy. But maybe there is a "watir-way".

Environment:

 # ruby 1.9.3p125 (2012-02-16) [i386-mingw32]
 # watir (4.0.2 x86-mingw32)
 # watir-classic (3.6.0, 3.5.0, 3.4.0)
 # watir-webdriver (0.6.4, 0.6.2)

Edit: i am aware that there are different ways to get images from the website, and without event thinking i could build a list with so much solutions, but its about to solve the problem with watir.

Heterophyllous answered 6/5, 2013 at 0:45 Comment(9)
Why is browser.image.save(file) useless? The method opens the save dialogue, inputs the required fields and save the file (ie the dialog is also automated). Or do you mean it is not helpful because it is just for IE and you need to use a different browser?Frankly
In my case it opens the save dialogue and waits. As described in the manual that should only happen if the file exists, but this is not the case (empty directory and random file name)Heterophyllous
That is odd. I have not run into any issues saving an image as a new file.Frankly
i ve added a not working exampleHeterophyllous
Does the `c:\tmp` directory exist? Your example only hangs for me if the directory does not exist.Frankly
tried with root directories like c:\ z:\ \ / /cygdrive/z c:/ z:/ and without ... always with the same result... which which version of ruby and watir are you using?Heterophyllous
I tested it on Ruby 1.9.3p194 with watir-classic 3.2 and IE8.Frankly
Does either answer to this question help you? #16191326Agra
What about mechanize?Dasteel
K
2

If you can't open an image once it was showed there is only one Watir way. You can take a screenshot of your image using Element screenshot extension. It will be like:

require 'watir-webdriver'
require 'watir/extensions/element/screenshot'

b = Watir::Browser.new
b.goto "http://your_page.com"
b.element(:id => "your_img").screenshot("Your_img.png")
Kwon answered 29/3, 2016 at 10:22 Comment(1)
You might run into an issue related to the window size with this (especially on headless browsers), see my answer here for a fix!Leann
Z
0

You may want to consider disabling images on the browser being controlled by Watir. Then you can find the URLs in the source and fetch the images for the first time using your code. The net effect should be the same as what you're trying to do.

Zephan answered 30/9, 2013 at 2:16 Comment(0)
E
-2

I'm not on windows, so unable to try a 'watir-way', but you could do this simply by calling out to curl or wget:

browser.images.each do |img|
  %x| curl #{img.src} -O #{img.src.split('/').last} |
end
Eec answered 17/9, 2013 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.