Save image with watir-webdriver
Asked Answered
D

4

0

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?

Danette answered 13/9, 2013 at 10:40 Comment(5)
Are you by chance trying to write a bot that attempts to get around a Captcha?Hoebart
@AbeHeward yes, but this only for testing purpouses)Danette
@AbeHeward so what do you think?Danette
I think most programmers are going to have a problem helping people with techniques to bypass captchas.Hoebart
@AbeHeward bypass is bad) i need only for testing on overload our service... maybe watir is bad ideaDanette
B
1

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.

Benedikta answered 23/9, 2013 at 10:5 Comment(2)
it help with image, but not with my captcha with depends on cookie) but this is not your problem)Danette
yeah, try tesseract-ocr :)Benedikta
G
1
require 'watir-webdriver'

And if you're doing it frequently, you can extend Watir's Image class with:

Watir::Image.class_eval do

    def save path_to_new_file
          #so that src can be opened without raising: 
          #Errno::ENOENT: No such file or directory @ rb_sysopen
        require 'open-uri' 

        open(path_to_new_file, 'wb') do |file|
          file << open(src).read
        end
    end

end

so it can be used as follows:

browser = Watir::Browser.start 'google.com'
image = browser.img
image.save 'our_images/hi.png'
Galasyn answered 26/10, 2015 at 23:19 Comment(0)
T
0

According to the watir-webdriver documentation, there is no Image#save method because it hasn't been implemented.

On the other hand, watir-classic does have a Image#save method. This is the example from the rdoc:

browser.image.save("c:/foo/bar.jpg")
Tavel answered 13/9, 2013 at 21:44 Comment(2)
but watir-classic doesn't support chrome ff etcDanette
Well, I can't refute that. Beyond a watir-classic solution, you could look into net/http or mechanize, which has methods to access images in the Page class.Tavel
P
0

This turned out to be a lil harder than it should be, but I needed to accomplish this since an image was only accessible when a valid session cookie is set. This is how I finally managed to accomplish this:

1. Install watir-extentions-element-screenshot

https://github.com/ansoni/watir-extensions-element-screenshot
You probably want to do gem install watir-extensions-element-screenshot.

2. Resize the browser window

This also works with headless phantomJS. After you initialize the browser, set the window size to something rather big to prevent a bug from happening when the image is larger than the browser window.

browser = Watir::Browser.new :phantomjs
browser.window.resize_to(1900, 1080)

3. Get the image element and screenshot it

In my case, the entire site is an image. Luckily, browser.html does show that the image is still encapsulated in an <img> tag, so we can access the image (in this example all images on the page) like so:

browser.elements( :tag_name => "img" ).each do |x|
    x.screenshot("file_name.png")
end

This will save the image to file_name.png. It's not the exact same file, rather a screenshot of it. But as far as image download is concerned, this is a perfect solution for me and hopefully for others too!

Portingale answered 18/11, 2019 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.