将图像保存在watir-webdriver中

时间:2013-01-08 16:38:21

标签: ruby watir watir-webdriver

我需要将图像从recaptcha保存到localhost磁盘,我使用watir-webdriver获取图像dom元素,但它不支持save方法,就像watir一样。那么如何将图像保存到我的磁盘? 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>

和ruby-code:

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

如何将图像文件保存到磁盘?

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:2)

您可以使用Ruby的open-uri,如下所示:

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

答案 2 :(得分:0)

Access-Control-Allow-Origin: *