rubywatirwatir-webdriver

Save image with watir-webdriver


How could i save image, which is loaded via watir-webdriver? All manuals and examples show only fetching src of image, and using open-uri saving it. But i need to save that image, which was generated when my page was loaded. How can i do this?

Could i use watir, and watir-webdriver at the same time? For example:

require 'watir-webdriver'
require 'watir'
@driver = Watir::Browser.new :firefox
@driver.goto (@base_url)
@img = @driver.image(id: 'CaptchaImage').save("2131.png")

How can i do something like this? Or else how to get it from cache?

Could anybody help me with it?


Solution

  • OpenURI will help you ..

    require "watir-webdriver"
    require "open-uri"
    
    b = Watir::Browser.new :chrome
    b.goto "http://stackoverflow.com/"
    
    File.open("target_file.jpg", 'wb') do |f|
     f.write open(b.img(:class, "sponsor-tag-img").src).read
    end
    

    Hope you are not doing anything bad.. :)

    Please let me know if it helped.