Examples

Screenshot of a particular element 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 a screenshot of the element and save to file

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");

var element = driver.FindElement(By.CssSelector("#example-card"));
var screenshot = ((ITakesScreenshot)element).GetScreenshot();
screenshot.SaveAsFile("element.png");

driver.Quit();

Find out how to take a full-page screenshot with C# and Selenium.

That was 14 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.