Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save images from a website (with watir)

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.

like image 705
inselberg Avatar asked May 06 '13 00:05

inselberg


1 Answers

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")
like image 93
Antesser Avatar answered Oct 07 '22 16:10

Antesser