It's a widespread need to take a screenshot of a live website. On a project I worked on recently, we had a legal requirement to take screenshots of forms that our users filled, as at the time they filled the forms, for consent documentation purposes.
There are many ways to do this manually. A straightforward way is to open the form and use the shortcuts Command-Shift-3 on macOS. But this is not good enough when you need to take these screenshots automatically, based on user actions, or even just as part of batch processes. I won’t want to take 1000 screenshots manually, but I would happily write a script to automate taking those screenshots.
Let’s explore some approaches to taking a screenshot of a webpage in Haskell. We will take screenshots of the APItoolkit.io which uses AI to help you manage, monitor, and document your APIs, so you always know what’s going on in your backends and can debug issues that come up immediately.
Side note: All the code examples are standalone Haskell shell scripts. Copy them into a file on a Unix environment and execute them like any other executable shell script.
This is possible due to the line ”#!/usr/bin/env stack” at the top of the file. Note, Haskell stack is required on your local machine and can be installed via GHCup.
Using Haskell Selenium WebDriver
Selenium is a well-known player in the QA and Web testing space. And with the web driver specification, we can control any browser. We will use the webdriver-w3c dependency for this test from hackage. Feel free to install that. Since we’re using a Haskell shell script, we only need to include the dependencies on the second line with the stack resolver:
This error is because we need to run a server process which our script can interact with. This process differs for different browsers. There is geckodriver, firefoxdriver, chromedriver, etc.
First, download the chromedriver from https://chromedriver.chromium.org/, and then we can start it in a separate process with the following command:
Terminal window
chromedriver--port=4444
Port 4444 is the default port for the WebDriverConfig we used in our Haskell program.
Next, we restart our Haskell shell program. It would spit out some logs, then spin up a chrome window, open the API toolkit on the window, take a screenshot and save it to screenshot.png.
Using the ScreenshotOne API
ScreenshotOne is a screenshot as a service API that lets us take screenshots using just their API and is also a viable option, especially when we don’t want to deal with the hassle of managing a chrome or other browser instance.
The code is quite straightforward, as we simply need to make a GET request to an endpoint. Let’s do this with the wreq Haskell library.
r <- get $"https://api.screenshotone.com/take?access_key="<> accessKey <>"&url="<> url <>"&device_scale_factor=1&format=jpg&cache=false"
B.writeFile "screenshot-api.png" (B.toStrict $ fromMaybe ""$ r ^? responseBody)
An API versus Selenium WebDriver and alternatives
I would advise trying Selenium WebDriver for quick prototyping, and if the volumes of screenshots are small, I mean counted by tens.
In addition, API usually has many other problems, like blocking pop-ups, banners, and ads and fixing full-page artifacts.
But if you expect to take screenshots constantly, it is better to examine using existing screenshot API. There is a vast choice nowadays of what to choose. Look at the list of the best screenshot APIs.
Summary
Pick the solution that fits your needs. But it’s helpful to know we have options, especially while building Haskell applications.
Read more posts
Interviews, tips, guides, industry best practices, and news.
With Python, you can take website screenshots in multiple ways. But the best way to do it depends solely on your needs and your use case. Let's quickly examine all the options.
The article examines how you can take screenshots of any URL with Java by using Selenium, Puppeteer alternatives, Playwright, or screenshot API as a service.
You can take a full page screenshot with Pupeeter by specifying the `fullPage` parameter as true when taking a screenshot. But there is a caveat. If a site has lazy-loaded images, they won't be rendered. Let's examine how to fix the issue and trigger lazy image loading with Puppeteer.
Read more
Automate website screenshots
Exhaustive documentation, ready SDKs, no-code tools, and other automation to
help you render website screenshots and outsource all the boring work related to
that to us.