Take Screenshots of Websites from the Linux Command Line
Both Google Chrome and Firefox have the ability to take screenshots from the Linux command line. using the browsers "headless" function. This can come in handy when creating scripts that take screenshots of a URL without user interaction as well as loading metadata (e.g. the DOM).
The term "headless" or "headless mode" basically means using a system or application that does not have a monitor or graphical user interface (GUI). During headless operation, the browser will not attempt to create a window because it assumes there is no GUI.
Taking Screenshots with Google Chrome / Chromium from Linux Command Line
Taking a screenshot of a URL in Google Chrome from the command line is fairly easy. Here is the basic syntax of the command:
[path/to/chrome] --headless --screenshot="[path/to/save/filename.png]" "[URL]"
NOTE: If you are using Chromium simply replace google-chrome with chromium-browser in the following commands.
The first step is to find the location of the google-chrome executable. We can do this by using the which command like so:
[mcherisi@putor ~]$ which google-chrome /usr/bin/google-chrome
Now that we have the location, we simply run chrome with some options like so:
/usr/bin/google-chrome --headless --screenshot="/home/mcherisi/chrome-ss1.png" "https://www.putorius.net"
Here is a sample screenshot taken with the above command.
As you can see from the screenshot above, Chrome chooses an odd window size by default. It's somewhere between a phone and a tablet, but oddly square. You may have also noticed that it only captures the first "above the fold" content. We can control the screen capture size using the
--window-size option. Here we set the width to be 1920 pixels (full HD) and the height to be 2000 pixels. We are also using the
--hide-scrollbars option to hide the scrollbars for a cleaner screenshot.
/usr/bin/google-chrome --headless --hide-scrollbars --window-size=1920,2000 --screenshot="/home/mcherisi/chrome-ss3.png" "https://www.putorius.net"
Now the resulting screenshot looks a lot more like what we would see on a desktop machine.
There are a lot of command line options when using Chrome or Chromium. You can use them to get the exact screenshot you want. See the Links and Resources section below for a complete list of command line options.
Taking Screenshots with Firefox from the Linux Command Line
Taking a screenshot with Firefox from the command is very straight forward. Firefox includes a handy
--screenshot option that automatically puts you in headless mode. The other advantage of using Firefox over Chrome, is that Firefox takes a screenshot of the full web page by default.
The basic syntax for take a screenshot with Firefox from the command line is:
[path/to/firefox] --screenshot "path/to/save/filename.png" "[URL]"
NOTE: If you omit the filename the file will be saved in the current working directory as screenshot.png.
You can use the which command to find the location of the Firefox executable just like we did above. here is an example command capturing a screenshot of putorius.net and saving the file as firefox-ss1.png in the current directory.
/usr/bin/firefox --screenshot firefox-ss1.png https://www.putorius.net
Here is the resulting screenshot.
You can also use the
--window-size option with Firefox if you want to limit the width or height. In the example below, we limit the width to 1200 and the height to 800.
/usr/bin/firefox --screenshot --window-size=1200,800 firefox-ss3.png https://www.putorius.net
That's it. Once you know the syntax it's simple to capture a website screenshot from the Linux command line. There are a ton of command line options for both browsers. You can learn more about the options and other uses by following the links below.
Resources and Links
This site uses Akismet to reduce spam. Learn how your comment data is processed.