Automate screenshot scraping with watir and ruby

require 'watir'

def resize_browser(browser)
 height = browser.execute_script("return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);")
 browser.window.resize_to(1200, height)
end

# Load from external csv
app_ids = [41109, 41110, 41112]
browser = Watir::Browser.new :chrome, headless: true
app_dir = 'apps' 
FileUtils.rm_rf(app_dir) if Dir.exist?(app_dir) 
Dir.mkdir(app_dir)

app_ids.each do |app_id|
 dir = "#{app_dir}/#{app_id}"
 Dir.mkdir(dir)
 
 tabs = ['one', 'two', 'three'] 
 tabs.each do |tab|
 browser.element(:css, "div[data-tab='#{tab}']").click
 resize_browser(browser)
 browser.screenshot.save("#{dir}/#{tab}.png")
 end
end

browser.close


Leave a Reply

Your email address will not be published. Required fields are marked *