Miyazaki, the 16. June 2022

Taking Website Screenshots with Ruby

You have a file with a list of urls and you want to screenshot each of those.

Install geckodriver from https://github.com/mozilla/geckodriver/releases

geckodriver.exe needs to be in the system path

Install the watir-screenshot-stich Gem

gem install watir-screenshot-stitch


require 'URI'
require 'fileutils'
require 'watir-screenshot-stitch'

basepath = File.join(__dir__, 'screenshots')
FileUtils.mkdir_p basepath

content = File.read('20220610_URLS.txt')

b = Watir::Browser.new :firefox

content.lines.each do |line|
    line = line.strip
        if  line.include?("http")
            uri =  URI.parse(line).normalize.to_s
            hostname = URI.parse(line).host 
            path = File.join(basepath, hostname)
            FileUtils.mkdir_p path
            puts uri
            b.goto uri
            png = b.screenshot.base64_geckodriver
            filename = uri.gsub(/[^0-9A-Za-z.\-]/, '_')
            path = File.join(path, "#{filename}.png")
            File.open(path, 'wb') { |f| f.write(Base64.decode64(png)) }
       # puts line + " is not url"

watir-screenshot-stich on Github

PS: There is also a gem on how to take screenshots of windows programs in ruby: https://rubygems.org/gems/win32screenshot

This could be extended to add the screenshots to word documents: https://rubywithwatir.blogspot.com/2008/07/how-do-i-take-screenshots-and-append-to.html

Thanks for reading

If you enjoyed this article, Just send out this e-mail. I will let you know next time I write something.