Save images from website (using watir)

How can I save site images using watir without reloading them with open uri or similar?

I: The reason I can’t use

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

lies in the fact that images are generated in the current session (login-) and only once, so the "external" 2nd access is impossible.

II: browser.images.save() - only for ie - is also not useful, it opens a save dialog. So it is 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 for atm is to get all images using a proxy. But maybe there is a vari-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 know there are different ways to get images from a website, and without taking into account events, I could create a list with so many solutions, but he decided to solve the problem using watir .

+6
source share
3 answers

You might want to consider disabling images in a browser controlled by Watir. Then you can find the URLs in the source and get the images for the first time using your code. The net effect should be the same as what you are trying to do.

0
source

If you cannot open the image as soon as it has been shown, there is only one way of Vatira. You can take a screenshot using the Element Screen Extension . It will look like this:

 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") 
0
source

I am not on windows, so I can not try "watir-way", but you can do it simply by calling curl or wget:

 browser.images.each do |img| %x| curl #{img.src} -O #{img.src.split('/').last} | end 
-2
source

Source: https://habr.com/ru/post/944332/


All Articles