How to Capture a Screenshot of a Page with Dynamic Content and Login using Golang


Capturing a screenshot of a login required page is sometimes hard than you think.

The author recommends this web automation tool called chromedp to do this task.

The Golang Chromedp package

A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol.

Simply speaking, the chromedp is a high-level Chrome DevTools Protocol client that simplifies driving browsers using the CDP.

It is a library for Go that allows you to automate web pages such as:

  • Web scraping
  • Fill out and submit a form
  • Download file and image
  • Capture a pdf of a page
  • Click on an element
  • Take a screenshot of a specific element and the entire browser viewport
  • Evaluate javascript and retrieve the result

Some Basic Concepts

Before we jump into the actual code, let’s clarify some concepts.

A static website

image from source

A static website has only simple static resource files, such as images, CSS, and JavaScript hosted on the server.

When a user visits a static website, the browser makes a request to the server for a particular page. Then the web server sends the webpage directly to the browser without modifying it.

A dynamic website

Dynamic websites are different; they contain code-generated content and are not static.

There are two types of dynamic websites:

  • client-side scripting (JavaScript) driven websites
  • server-side scripting (PHP, Python, Ruby, etc.) driven websites

A Required Login website

A Required Login website is a website that requires a user to log in before they can access the website.

Gmail is an example of a login-required website.

Photo by Stephen Phillips – Hostreviews.co.uk on Unsplash

Getting hands dirty

Our target website today is a Google Analytics Marketing Website that tracks a series of metrics, as shown below.