Guardar imagen con 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?

preguntado el 13 de septiembre de 13 a las 11:09

Are you by chance trying to write a bot that attempts to get around a Captcha? -

@AbeHeward yes, but this only for testing purpouses) -

@AbeHeward so what do you think? -

I think most programmers are going to have a problem helping people with techniques to bypass captchas. -

@AbeHeward bypass is bad) i need only for testing on overload our service... maybe watir is bad idea -

4 Respuestas

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.. :)

Por favor, avíseme si le ayudó.

Respondido el 23 de Septiembre de 13 a las 11:09

it help with image, but not with my captcha with depends on cookie) but this is not your problem) - brabertaser19

yeah, try tesseract-ocr :) - Sveatoslav

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'

Respondido 26 Oct 15, 23:10

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

Respondido el 13 de Septiembre de 13 a las 22:09

but watir-classic doesn't support chrome ff etc - brabertaser19

Well, I can't refute that. Beyond a watir-classic solution, you could look into net / http or mecanizar, which has methods to access images in the Page clase. - orde

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
Probablemente quieras hacer 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!

respondido 18 nov., 19:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.