Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save image in watir-webdriver

I need to save image from recaptcha to localhost disk, i'm getting image dom element using watir-webdriver, but it doesn't support save method, as watir do. So how can i save image to my disk? Html:

<div id="recaptcha_image" style="width: 300px; height: 57px;">
  <img style="display:block;" alt="Проверка по слову reCAPTCHA" height="57" width="300" src="https://www.google.com/recaptcha/api/image?c=03AHJ_VusSUxF0IYURRcVTVTjJJnUk92j-hXYsuwqvu0m5tvKFzAnwvrHlz-j_Gfqg-sUrHLj3D2DrUYNNg4uvr2BNgZqlK5vpJUJVYkkWo36I4RRmRGkYZru5kBYhzPCCn49KlH6wW_iLw6vIzv7vnhpu6ndqxb-9SkIRrVYyBwN39kg18Lov7Hc">
</div>

and ruby-code:

cap = @browsers[i].div(:id => 'recaptcha_image').image

How to save image file to disk?

like image 244
byCoder Avatar asked Jan 08 '13 16:01

byCoder


2 Answers

require 'watir-webdriver'    
require 'open-uri'

image_src = @browsers[i].div(:id => 'recaptcha_image').image.src

File.open("/path/", 'wb') do |f|
  f.write open(image_src).read
end
like image 131
Eugene Rourke Avatar answered Nov 10 '22 19:11

Eugene Rourke


You can use Ruby's open-uri as follows:

require 'open-uri'

url = "the image url" #https://www.google.com/recaptcha/api/image?c=03A....

File.open("./image.jpg", "wb") do |file_write|
  open(url, 'rb') do |file_read|
    file_write.write(file_read.read)
  end
end
like image 2
Jiří Pospíšil Avatar answered Nov 10 '22 20:11

Jiří Pospíšil