Save image in watir-webdriver

时间秒杀一切 提交于 2019-12-03 15:07:45
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

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
    require 'watir-get-image-content'
    def save_captcha(@browser)      
        img = @browser.image(xpath: '//*[@id="ctl00_captcha"]/td[2]/div[1]/span[1]/img')        
        filename = "#{@path}/tmp/current_captcha.jpg"
        File.open(filename, 'wb'){|file| file.write( img.to_jpg) }          
    end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!