ScreenshotNeo

BlogHow-to

How to Set Up Selenium in Visual Studio

Install Selenium in a Visual Studio C# project, launch Chrome, and run a first browser interaction. Includes NuGet, CLI, troubleshooting, and next steps.

By the ScreenshotNeo team4 October 20269 min read

To set up Selenium in Microsoft Visual Studio, create or open a C#/.NET project, add the Selenium.WebDriver NuGet package, then run a small program that starts Chrome with ChromeDriver. Selenium Manager handles driver and browser management by default, so a separate manual driver download is not required for every setup.

This guide covers a one-off browser smoke test in a .NET project. Visual Studio is the IDE; Visual Studio Code is a separate editor and its extensions are not part of these steps. Selenium’s .NET client and examples are maintained in the official Selenium installation guide. Check the Selenium downloads page for the current package version when updating an existing project.

1. Check the prerequisites

  • Microsoft Visual Studio with a C# workload suitable for the project you are creating.
  • A .NET SDK installed and available to the project. Selenium’s current .NET examples use SDK-style projects.
  • A C# project in which you can add NuGet packages.
  • A supported browser installed, such as Google Chrome, if you plan to run the example below.

Exact Visual Studio menus and SDK compatibility can vary by edition and release. If project creation or the SDK is the part that is failing, check the documentation for your installed Visual Studio and .NET SDK versions.

2. Install Selenium.WebDriver

Option A: Visual Studio NuGet Package Manager

  1. Open the solution and select the C# project that will run the automation.
  2. Open that project’s NuGet package manager.
  3. Search for Selenium.WebDriver, select the Selenium project’s package, and install it into the intended project.
  4. Build the project so package restore runs and any compilation issues appear before you start the browser.

Installing the package into the wrong project in a multi-project solution is a common source of “namespace not found” errors. Confirm the target project before installing.

Option B: .NET CLI

Open a terminal in the directory containing the target .csproj and run:

dotnet add package Selenium.WebDriver

Then restore and build:

dotnet restore
dotnet build

Option C: Add a PackageReference

You can also add the package reference directly to an SDK-style project file. Use the current version shown on Selenium’s downloads page rather than treating an example version as permanent:

<ItemGroup>
  <PackageReference Include="Selenium.WebDriver" Version="CURRENT_VERSION" />
</ItemGroup>

Replace CURRENT_VERSION with the version you intend to pin, then restore and build. The CLI and Visual Studio NuGet manager both update the project’s package references for you.

3. Run a first Selenium browser session

Create a console app or use an existing executable C# project. Put this in its entry point. The example opens Selenium’s sample web form, enters text, submits it, prints the result, and closes the browser even if an action fails.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

var options = new ChromeOptions();

IWebDriver driver = new ChromeDriver(options);

try
{
    driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/web-form.html");

    var title = driver.Title;
    Console.WriteLine($"Page title: {title}");

    var textBox = driver.FindElement(By.Name("my-text"));
    textBox.SendKeys("Selenium from Visual Studio");

    driver.FindElement(By.TagName("button")).Click();

    var message = driver.FindElement(By.Id("message")).Text;
    Console.WriteLine($"Form result: {message}");
}
finally
{
    driver.Quit();
}

Run the project from Visual Studio. On the first run, Selenium Manager may need network access to determine and obtain a compatible driver or browser component. The Selenium project describes Selenium Manager as the default automated browser and driver management tool in its overview.

What the code does

  • ChromeOptions configures Chrome. The empty options object is sufficient for the basic local run.
  • ChromeDriver creates a WebDriver session.
  • Navigate().GoToUrl() opens a page.
  • FindElement locates a control, and SendKeys and Click interact with it.
  • Quit() ends the session and closes the browser. Put cleanup in finally so it runs after exceptions too.

The sample is a console smoke test, not a test framework. It proves that the package can compile and a browser session can start, navigate, interact, and shut down.

4. Choose the right setup for your use case

Need Good starting point Consider
One quick browser interaction Console app and the sample above Keep the browser lifecycle in one method and always call Quit().
Repeatable checks in a codebase A .NET test project and a test runner Use setup and teardown around each test or fixture, and keep assertions in the tests.
Run in a restricted or remote environment Review browser availability, network access, and the execution environment first Local driver management may not fit a locked-down machine or a remote browser grid.

A test runner is useful when checks need organization, assertions, reporting, and repeatable execution. Selenium’s test organization guidance discusses runners and .NET examples; it does not make a particular runner mandatory for a first script. Select a runner already supported by your team’s .NET environment and follow that framework’s current setup instructions.

5. Browser and driver configuration

For the common local Chrome case, start with Selenium Manager’s default behavior. Add configuration only when the environment or test requires it.

Headless Chrome

For environments without a visible desktop, request headless mode through Chrome options:

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

IWebDriver driver = new ChromeDriver(options);

Headless behavior and available Chrome flags depend on the installed browser version and environment. If the browser exits at startup, inspect the full driver error and verify that Chrome itself can start under the same account.

Other browsers

Selenium provides browser-specific driver classes and options. For example, Firefox uses FirefoxDriver with FirefoxOptions. Install and configure the relevant browser, then consult the current Selenium documentation for that browser and environment. Avoid copying Chrome-specific arguments into another browser’s options.

When manual driver setup is actually needed

Selenium Manager is the default, but a locked-down network, controlled build image, or organization-managed browser may require an explicit driver arrangement. First check whether the machine can access the browser and the resources Selenium Manager needs. If it cannot, follow the current Selenium guidance for explicitly managed drivers or a remote WebDriver. Do not assume a manually downloaded driver is the universal first step.

6. Make interactions more reliable

  • Prefer stable locators. Use a site’s stable IDs, names, or purpose-built test attributes where available. Avoid selectors tied to incidental layout classes.
  • Wait for the condition you need. Pages and client-side apps may render asynchronously. Use Selenium waits for a specific element or state rather than adding arbitrary long sleeps.
  • Keep each session bounded. A browser session consumes resources. Close it in cleanup and avoid sharing one driver across unrelated parallel tests.
  • Separate setup from assertions. In a suite, create reusable setup and teardown and make each test verify a clear outcome.
  • Keep credentials out of source control. If a test needs authentication, load secrets from an appropriate local or CI secret store rather than hard-coding them.

7. Troubleshooting

Symptom Likely cause What to check
The type or namespace name 'OpenQA' could not be found The package was not added to this project, restore failed, or the wrong project is selected. Check the target .csproj for Selenium.WebDriver, restore packages, and rebuild the project that contains the source file.
Chrome does not start or driver session creation fails Chrome is unavailable, cannot run under the current account, or driver/browser setup could not complete. Confirm Chrome is installed and launches for this account. Read the complete Selenium Manager and driver error; check network restrictions and permissions.
First launch hangs or fails while resolving a driver The environment may block the network access Selenium Manager needs, or have restricted file permissions. Check proxy/firewall rules and write permissions for the process environment. In controlled environments, configure a permitted driver or remote browser approach using current Selenium documentation.
NoSuchElementException The selector is wrong, the element is not on the current page, or rendering has not completed. Verify the URL and locator against the current DOM. Wait for the target element to be present before interacting.
The click happens but the expected content is missing The next page or result is asynchronous, or the click targeted a different control. Wait for a result-specific condition, then inspect the page state and locator. Avoid assuming that a click means the page has finished updating.
The browser stays open after an exception Cleanup did not run or the session was never closed. Put Quit() in a finally block. In a test project, use the framework’s teardown lifecycle.
Works locally but fails in CI CI may lack a desktop, browser, network access, or required permissions. Check the CI image and account, use headless mode only when appropriate, and capture the full startup error in build logs.

When investigating a failure, retain the complete exception and inner exception. A shortened message often omits whether the problem occurred during package restore, browser discovery, driver startup, navigation, or element lookup.

8. Performance, reliability, and cost

A Selenium run starts and controls a real browser, so it uses more time and machine resources than a direct HTTP request. Keep suites focused, reuse setup where the test framework allows it safely, and do not start unnecessary browser sessions. Parallel sessions can increase throughput but also multiply CPU and memory demand; tune concurrency to the available runner capacity.

For reliability, make tests wait on observable page conditions, use stable selectors, isolate test data, and always close browser sessions. Browser and driver behavior can change as browser versions and Selenium packages change, so keep dependencies intentional and investigate version updates against the official Selenium pages.

The Selenium package is installed through NuGet. The sources cited here do not specify a price for the package or a particular hosting cost. Your practical cost comes from development and execution resources, such as CI time and the machines or browser infrastructure used to run sessions.

Or skip the browser setup

If the job is to capture a page rather than interact with it, ScreenshotNeo offers a website screenshot API and MCP server. One GET request can return PNG, JPEG, WebP, or PDF. See the API documentation.

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

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
const image = Buffer.from(await res.arrayBuffer());
await import('node:fs/promises').then(fs => fs.writeFile('shot.webp', image));
  • Cookie and consent banners are accepted like a visitor, and more than 60 known consent platforms, newsletter popups, and chat widgets are removed before the shot; each step can be turned off.
  • Bot checks, CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed. Responses include X-Page-Verdict and X-Billed headers.
  • An MCP server gives AI agents tools for screenshots, page information, and PDF capture.
  • The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.

Create a free ScreenshotNeo account for 1,000 screenshots a month with no card.

FAQ

Do I need to install a separate ChromeDriver?

Usually not for a standard setup. Selenium Manager handles browser and driver management by default. A restricted environment may need an explicitly managed driver or remote browser.

Is Selenium a Visual Studio extension?

The .NET Selenium client is a NuGet package added to your C# project. Visual Studio is where you edit and run that project.

Do I need a test framework to try Selenium?

No. A small console program can verify the first browser session. A test runner becomes useful when you need repeatable cases, assertions, setup and teardown, and reports.

Can Selenium take a screenshot?

Yes. WebDriver includes screenshot capabilities for the current browser view. If you need a screenshot service that returns an image or PDF from a URL without setting up a local browser session, use the ScreenshotNeo API example above.

Official references