Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

A NullReferenceException during a Selenium screenshot call usually means your C# code tried to use a reference whose value is null; it does not, by itself, mean the browser or screenshot service failed. Check the exact line in the stack trace, then inspect the WebDriver, the ITakesScreenshot reference, and the returned Screenshot separately. If Selenium reports a WebDriverException instead, investigate screenshot support in the concrete driver or wrapper.

What the exception means

Microsoft defines NullReferenceException as an exception thrown when code tries to access a member on a type whose value is null. In a screenshot expression such as ((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(path), several operations are chained together. The exception tells you that a reference used in that expression was null; it does not identify which reference without the failing line and stack trace.

Separate that problem from screenshot capability errors. Selenium documents WebDriverException for the screenshot support extension when a driver does not support the operation. A missing screenshot capability and a null reference in your own calling code are different failure branches and call for different fixes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Find the exact null reference

  1. Read the exception and stack trace. Locate the first stack frame in your code and the precise source line. If several operations are on one line, split them so the failing operation is unambiguous.
  2. Check initialization and lifetime. Verify that the driver was assigned before the screenshot code runs, and that the screenshot is not being requested after test teardown has disposed of or cleared the driver reference.
  3. Check screenshot support explicitly. Determine whether the actual driver object implements ITakesScreenshot. The Selenium base WebDriver implements this interface, but a custom wrapper or another IWebDriver implementation must be checked rather than assumed to expose it.
  4. Check the result before saving. Store the value returned by GetScreenshot() in a local and verify that the exception is not occurring when calling SaveAsFile.
  5. Classify the exception type. A NullReferenceException points to a null dereference in the calling path. A WebDriverException raised by Selenium’s screenshot extension points instead to driver capability or API behavior.

For diagnosis, make each step visible in code rather than debugging a long chained expression:

Use Selenium’s screenshot interface with explicit checks

Selenium’s .NET screenshot interface is ITakesScreenshot. Its GetScreenshot() method returns a Screenshot, which can be saved as a PNG. The following uses an existing driver variable; create and configure that driver earlier in the test using the implementation your project already uses.

using OpenQA.Selenium;

// The driver must already be initialized and still be alive here.
if (driver is null)
{
    throw new InvalidOperationException("WebDriver was not initialized before screenshot capture.");
}

if (driver is not ITakesScreenshot takesScreenshot)
{
    throw new NotSupportedException("This WebDriver does not support screenshots.");
}

Screenshot screenshot = takesScreenshot.GetScreenshot();
screenshot.SaveAsFile("screenshot.png", ScreenshotImageFormat.Png);

The interface check prevents you from silently assuming that every custom IWebDriver implementation supports screenshots. The explicit locals also make the failing operation easier to identify. If driver is null, fix the test setup, dependency injection, or object lifetime; if the interface check fails, inspect the concrete driver or wrapper. If the exception occurs at the save call, use the stack trace to confirm which receiver is null rather than adding a null-conditional operator to the whole chain.

Keep capture inside the driver’s lifetime

A frequent setup-level cause is taking a screenshot in a cleanup or failure hook after teardown has already run. Keep the capture in the portion of the test where the driver is initialized and available. If your test framework has setup and teardown hooks, inspect their order and ensure the screenshot handler runs before the driver is disposed or the field is reset.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not hide required failures with null-conditional calls

Using driver?.GetScreenshot() is not a meaningful repair if the test requires an image for diagnosis. It can suppress the immediate exception and leave you without the evidence the failing test needs. Prefer a guard with an explanatory exception when a driver or screenshot is required. Handle a null value as an expected state only when the application or test design defines what should happen in that case.

Distinguish a null dereference from unsupported screenshots

What you see What it indicates What to inspect
NullReferenceException in your code A reference used by the failing expression has a null value. The failing source line, driver initialization and lifetime, interface reference, and screenshot result.
WebDriverException from the screenshot extension Selenium reports that the driver lacks screenshot support or the screenshot operation failed through its API path. The concrete driver or wrapper and whether it supports ITakesScreenshot.

Do not treat the second row as a nullability fix. Adding a null check will not add screenshot support to an implementation that does not provide it. Conversely, changing drivers is premature if the stack trace shows that your own driver variable is null before Selenium is called.

Prevent future null-reference failures

C# nullable reference types provide annotations and compile-time flow analysis that can warn about potentially null references. They do not change runtime behavior: a program can still throw NullReferenceException if a null reference is dereferenced. In a project where nullable analysis is appropriate, enable it in the project file:

<PropertyGroup>
  <Nullable>enable</Nullable>
</PropertyGroup>

Then treat warnings as prompts to make initialization and optionality explicit. Give required driver fields a valid value during setup, annotate values that may legitimately be absent with ?, and check or handle those optional values before use. Do not silence a warning just to make the build pass; establish why the value can be null and choose the behavior that fits the test.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check package and API versions

Selenium APIs and package behavior can vary by version. Check the versions of Selenium.WebDriver and Selenium.Support in the project and use documentation that matches those versions. The official API references consulted for this article were current on September 30, 2026; that date does not guarantee that their signatures or behavior match an older project. The exact failing cause cannot be determined from the exception name alone without the stack trace, source line, Selenium version, and concrete driver implementation.

Troubleshooting common cases

The driver variable is null

Cause: Test setup did not assign it, assignment failed or was skipped, or the reference was cleared before capture. Fix: Trace where the driver is created and assigned, and ensure the screenshot runs only after successful initialization and before teardown. Keep the guard so a setup defect produces a direct, understandable message.

The driver exists but the screenshot interface is unavailable

Cause: The object behind your IWebDriver variable may be a wrapper or implementation that does not expose ITakesScreenshot. Fix: Check the runtime object with the pattern match shown above. Use a driver implementation that provides screenshot support, or revise the wrapper to expose the capability if that is appropriate for your design. Do not assume that the interface is available solely because the variable is typed as IWebDriver.

The failure appears after the test fails

Cause: A failure hook may be trying to capture after cleanup has disposed of the browser or reset the driver field. Fix: Reorder the capture and teardown so the driver is still available when evidence is collected. If setup itself failed, handle that path separately rather than assuming a driver exists.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The exception is actually a WebDriverException

Cause: Selenium’s screenshot extension reports a driver capability or operation failure, not a C# null dereference. Fix: Check the concrete driver and wrapper for screenshot support and consult the documentation matching the project’s Selenium package version. Preserve the exception and stack trace; replacing it with a generic null check obscures the real branch.

Nullable warnings remain after enabling analysis

Cause: The compiler cannot establish that a reference is initialized on every path, or a value is intentionally optional but not annotated as such. Fix: Initialize required values at setup, use nullable annotations for genuinely optional references, and add a deliberate guard where the value is required at runtime. Nullable analysis reduces risk but is not a substitute for runtime checks at boundaries.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your actual goal is to capture a URL rather than exercise a Selenium browser session, ScreenshotNeo offers a one-request screenshot API. This is an alternative capture path, not a repair for a failing Selenium test. Its [documentation](https://screenshotneo.com/docs/) describes the available request options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each of those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and the response identifies the page verdict and billing status in headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf for AI agents. The free plan includes 1,000 screenshots a month without a card; paid plans start at $5 for 3,000 shots.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Sign up for ScreenshotNeo’s free plan to try it with 1,000 screenshots a month and no card.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.