Question

Is there a way to get an image extension (based on the content-type header) and it's body in Watir?

Here is an example

require 'watir'

zz = Watir::IE.new
zz.goto('http://flickr.com')
image = zz.image(:src => %r/l.yimg.com\/g\/images\//)
puts image

I need to get extension and the contents (base64encoded or just location of a temp file) of the latter image

Was it helpful?

Solution

require 'watir-webdriver'
require 'open-uri'
b = Watir::Browser.new :firefox

b.goto "http://altentee.com"
b.images.each do |img|
  uri = URI.parse(img.src)
  open(uri) { |file| puts file.content_type; open('/tmp/file', 'wb') { |tmp| tmp.write(file.read)}  }
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top