Watir - 当我尝试截取div

时间:2018-01-14 11:12:48

标签: ruby watir

我使用Watir和Watir-element-screenshot来获取元素的截图。 浏览器:Chrome和Firefox(最新版本) 我的代码:

require 'watir'
require 'watir/extensions/element/screenshot'
browser = Watir::Browser.new :firefox
browser.goto 'google.com.ua'    
browser.text_field(id: "lst-ib").set "ruby\n"    
browser.window.maximize
browser.element(id: "resultStats").screenshot("2.png")   
browser.close

并接受此错误:

C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/color.rb:968:in `scanline_bytesize': undefined method `>>' for 2439.0:Float (NoMethodError)
Did you mean?  >
    from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:207:in `encode_png_image_pass_to_stream'
    from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:174:in `encode_png_image_without_interlacing'
    from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:161:in `encode_png_pixelstream'
    from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:85:in `to_datastream'
    from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/image.rb:62:in `to_datastream'
    from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:35:in `write'
    from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:43:in `block in save'
    from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:43:in `open'
    from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:43:in `save'
    from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/watir-extensions-element-screenshot-0.0.3/lib/watir/extensions/element/screenshot.rb:12:in `screenshot'
    from 2.rb:11:in `<main>'

此消息的原因是什么以及如何修复它?

1 个答案:

答案 0 :(得分:0)

您可以使用此代码拍摄屏幕截图并保存在特定位置。

 browser.screenshot.save("path to save image/2.png")