Examples

How to take a full page screenshot with C# and Selenium

Install Selenium for .NET

dotnet add package Selenium.WebDriver
dotnet add package Selenium.Support

Selenium.WebDriver 4.11+ includes Selenium Manager, which downloads the right chromedriver automatically. Selenium.Support is only needed for WebDriverWait in the wait example. The examples use C# top-level statements — put them in Program.cs of a console project and run with dotnet run.

Take full page screenshot and save to file

using System.Drawing;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

var options = new ChromeOptions();
options.AddArgument("--headless=new");

using var driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://www.htmltoimage.com/demo");

// Chrome can't natively capture beyond the viewport, so size the window
// to the full height of the document first.
var width = Convert.ToInt32(driver.ExecuteScript("return document.documentElement.scrollWidth"));
var height = Convert.ToInt32(driver.ExecuteScript("return document.documentElement.scrollHeight"));
driver.Manage().Window.Size = new Size(width, height);

var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile("full-page.png");

driver.Quit();

Find out how to change the size of the viewport with C# and Selenium.

That was 20 lines of code — plus a browser to run in production

Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:

curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com"

Try the Urlbox screenshot API — free trial, no browser babysitting.