How to Take Screenshot of a Website From Url Using Ruby?

Hello Friends Today, through this tutorial, I will tell you How to Take Screenshot of a Website From Url Using Ruby? In Ruby, you can use the `watir` gem along with a web driver like `headless` to take a screenshot of a website. Here’s an example Ruby script:

First, make sure you have the required gems installed. You can install them using the following commands:

gem install watir
gem install webdrivers
gem install headless

Now, create a Ruby script (e.g., `screenshot.rb`) with the following content:

require 'watir'
require 'webdrivers'
require 'headless'

# Replace '' with the URL of the website you want to capture
url = ''

# Set the file name for the screenshot
output_file = 'screenshot.png'

# Start headless browser
headless =

# Initialize Watir browser with headless option
browser = :chrome, headless: true

# Navigate to the URL

# Capture screenshot
puts "Screenshot saved to: #{output_file}"

# Close the browser and stop headless mode

Make sure to replace:

– `` with the actual URL of the website you want to capture.
– `screenshot.png` with the desired output file name.

Run the script:

ruby screenshot.rb

This script uses `Watir` to automate the browser and `Headless` to run the browser in headless mode. Adjustments to the browser type, options, and other settings can be made based on your requirements.

Note: Taking screenshots of websites may have legal and ethical implications, and it’s important to respect the terms of service of the website. Additionally, web scraping or automated interactions with websites should comply with relevant laws and regulations.