Save All Images on a Webpage

Save All Images on a Webpage

If you need to save all of the images from a webpage you can take advantage of the save method in Watir::Image. This is really easy to do as you can see in the code below.

require 'watir'

browser = Watir::IE.new
browser.goto('http://twitter.com')

idx = 0

# using the images collection, iterate through all of the images on a page
browser.images.each do |x|
  idx += 1
  # apparently the string accepted by the string method will not allow variable substitution
  location = 'c:\tmp\file-' + idx.to_s + '.jpg'
  x.save(location)
end
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.