Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bidiu/c5fd385f8d7a94a40b9ed67e819337e3 to your computer and use it in GitHub Desktop.
Save bidiu/c5fd385f8d7a94a40b9ed67e819337e3 to your computer and use it in GitHub Desktop.
How to take full-page screenshots with Selenium and Google Chrome in Ruby
#!/usr/bin/env ruby
require 'selenium-webdriver'
wd = Selenium::WebDriver.for :remote, url: 'http://10.3.1.7:4444/wd/hub', desired_capabilities: :chrome
wd.navigate.to 'https://snipt.net/restrada/python-selenium-workaround-for-full-page-screenshot-using-chromedriver-2x/'
# Get the actual page dimensions using javascript
#
width = wd.execute_script("return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);")
height = wd.execute_script("return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);")
# Add some pixels on top of the calculated dimensions for good
# measure to make the scroll bars disappear
#
wd.manage.window.resize_to(width+100, height+100)
img = wd.screenshot_as(:png)
File.open('full-page.png', 'w+') do |fh|
fh.write img
end
wd.quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment