Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Learn Selenium with C# in this tutorial. Discover how to automate web testing, navigate pages, interact with elements, and validate results step by step.

Andreea
May 11, 2026
Selenium supports C#, Java, Python, and several other languages. C# is a natural fit for .NET teams: it runs on Visual Studio, integrates with NUnit, and keeps test code in the same ecosystem as the application.
Overview
Selenium is an open-source browser automation framework. Its C# bindings give .NET developers access to the full WebDriver API directly from Visual Studio, using the same language as their application code.
Why choose C# for Selenium testing?
C# ( or C-Sharp) is an object-oriented language that runs on the .NET framework. It works with the Common Language Runtime (CLR).
In C#, software applications can be split into smaller components using functions. This makes C# a structured programming language.
It is chosen for Selenium testing because it's a modern, statically typed, object-oriented language with easy-to-read syntax, which simplifies writing and maintaining tests.
It provides a wide array of features for automation and comes with a rich set of libraries and frameworks, such as NUnit. These resources support testing across different types of applications, including desktop, web, mobile, and games.
Subscribe to the TestMu AI YouTube Channel for more Selenium C# tutorials.
Prerequisites
|
Before you get started, you need to have a code editor installed on your machine. For this Selenium C# tutorial, let’s use Visual Studio. However, you can use any IDE of your choice.
You can also check out this blog to set up Selenium in Visual Studio.
Follow the below steps to create a new project in Selenium with C#:



If you want to see and run the tests in the Visual Studio, you need to follow the below steps:

Note: Run C# automated tests with Selenium at scale on TestMu AI. Try TestMu AI free!
The following are the prerequisites to run tests on the local grid:
To add a NuGet package to the project, please follow the below-mentioned steps:

Alternatively, you can install NuGet packages from the command line by running this command in the project location:
dotnet add package Selenium.WebDriver --version 4.43.0Selenium 4 vs Selenium 3: Selenium 4 switches to the W3C WebDriver protocol by default. The DesiredCapabilities class is removed - use browser-specific Options classes instead (e.g., ChromeOptions). The AddAdditionalCapability() method is replaced by AddAdditionalOption(), which is what the TestMu AI cloud config uses.
One thing to make sure of here is to install the same driver version as the browser you have installed on your machine.
You can use the browser of your choice, and you can add the drivers the same way, for example, Selenium.WebDriver.MSEdgeDriver (for Microsoft Edge), Selenium.WebDriver.GeckoDriver (for Mozilla Firefox).
For the test scenario, let’s use the TestMu AI Selenium Playground.
Test Scenario:
|
Test Implementation:
Now, we should be ready to start our Selenium C# test. Let’s use the previously created class and rename it to SeleniumTests.
public class SeleniumTests
{ private IWebDriver driver;
[SetUp]
public void Setup()
{
driver = new ChromeDriver();
}
[Test]
public void ValidateTheMessageIsDisplayed()
{
driver.Navigate().GoToUrl("https://www.testmuai.com/selenium-playground/simple-form-demo");
driver.FindElement(By.Id("user-message")).SendKeys("TestMu AI rules");
driver.FindElement(By.Id("showInput")).Click();
Assert.IsTrue(driver.FindElement(By.Id("message")).Text.Equals("TestMu AI rules"),
"The expected message was not displayed.");
}
[TearDown]
public void TearDown()
{
driver.Quit();
}
}
}
Code Walkthrough:
Import packages at the beginning of the test class. Then, create a new instance of the IWebDriver.
As a precondition to the test, start the browser. For this tutorial, it is done using the [SetUp] method because it’s something that will be used in all future tests, so here is where the driver is instantiated.

Next, there is our actual test, where the previously mentioned [Test] attribute from NUnit is used, and inside the test method, the test steps.

To navigate to a URL in Selenium with C#, use the Navigate().GoToUrl() method. When you need to interact with page elements, you can locate them using the FindElement() method, and it's best to go with the ID locator since it’s quicker.
After that, you can enter text using the SendKeys() method, click buttons using the Click() method, and check if everything worked as expected using Assert.IsTrue() method, adding a custom message for easier debugging if something goes wrong.
To close the browser at the end of the test, use the [TearDown] attribute and the quit() method.
Selenium C# method quick reference:
| Method / Property | Purpose |
|---|---|
| Navigate().GoToUrl(url) | Open a URL in the current browser session |
| FindElement(By) | Locate the first matching DOM element |
| FindElements(By) | Return all matching elements as a list |
| SendKeys(text) | Type text into an input element |
| Click() | Simulate a mouse click on an element |
| .Text | Read the visible text content of an element |
| Quit() | Close all browser windows and end the WebDriver session |
Test Execution:
Now that the test is ready, you can build the project (right-click on the project name and select Build or use the Ctrl+B shortcut) and run the test locally. The test will be visible in the Test Explorer:
If everything goes well, the test will pass, and the browser window will close at the end. The Test Explorer output looks like this:
Total: 1, Passed: 1, Failed: 0, Skipped: 0
ValidateTheMessageIsDisplayed [2.3s] PASSED
What if you want to run the automated tests cross-platform or cross-browser? Or using a setup different from your own machine?
These situations likely happen often because we want our web applications to work for all our users, and they will have various configurations set up on their computers.
The solution is porting your existing Selenium C# tests to a cloud-based Selenium Grid. TestMu AI provides an online Selenium Grid with 10,000+ real devices and browsers to run Selenium C# tests at scale, without managing local infrastructure.
The best part is that porting from a local Selenium Grid to the TestMu AI cloud grid requires minimal code changes. To run our tests on the cloud, let’s use the same Simple Form Demo test scenario from the Selenium Playground.
Test Implementation:
Here’s the test script configured to run on the TestMu AI cloud grid.
private static IWebDriver driver;
public static string gridURL = "@hub.lambdatest.com/wd/hub";
public static string LT_USERNAME = "LT_USERNAME"; // Add your TestMu AI username here
public static string LT_ACCESS_KEY = "LT_ACCESS_KEY"; // Add your TestMu AI access key here
[SetUp]
public void Setup()
{
ChromeOptions capabilities = new ChromeOptions();
capabilities.BrowserVersion = "latest";
Dictionary<string, object> ltOptions = new Dictionary<string, object>();
ltOptions.Add("username", LT_USERNAME);
ltOptions.Add("accessKey", LT_ACCESS_KEY);
ltOptions.Add("platformName", "Windows 10");
ltOptions.Add("project", "SeleniumTutorial");
ltOptions.Add("build", "Selenium CSharp");
ltOptions.Add("w3c", true);
ltOptions.Add("plugin", "c#-nunit");
capabilities.AddAdditionalOption("LT:Options", ltOptions);
driver = new RemoteWebDriver(new Uri($"https://{LT_USERNAME}:{LT_ACCESS_KEY}{gridURL}"), capabilities);
}
Code Walkthrough:
When using TestMu AI, you need your Username and Access Key. Find them in your Account Settings > Password & Security.

A good idea is to store them as environment variables and read them from there. This way, in case any of the values change, you only need to update them in one place.
The biggest change, as you can see, is in the [Setup] method. ChromeOptions is a Selenium class used to configure browser attributes for cross browser testing using Chrome.

Use the TestMu AI Automation Capabilities Generator to build your desired capabilities without writing them by hand.
The remaining test script will remain unchanged.
Test Execution:
Run the test from Visual Studio as before. This time the browser does not open locally. You will see the live test execution in the TestMu AI Web Automation Dashboard instead.

To integrate these tests into a CI/CD pipeline, use HyperExecute to run your NUnit suite in parallel across multiple browser and OS combinations with a single YAML config. For broader multi-browser strategy, the cross-browser testing guide explains how to structure your capability matrix.
These five practices separate maintainable Selenium C# test suites from fragile ones. Each includes a working code snippet you can apply directly to the Selenium Playground tests above.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement button = wait.Until(
ExpectedConditions.ElementToBeClickable(By.Id("showInput")));
button.Click();public class SimpleFormPage
{
private IWebDriver driver;
private By messageInput = By.Id("user-message");
private By showInputBtn = By.Id("showInput");
private By messageOutput = By.Id("message");
public SimpleFormPage(IWebDriver driver) { this.driver = driver; }
public void EnterMessage(string text) => driver.FindElement(messageInput).SendKeys(text);
public void ClickShow() => driver.FindElement(showInputBtn).Click();
public string GetMessage() => driver.FindElement(messageOutput).Text;
}These four exceptions account for the majority of Selenium C# failures. Each entry includes what triggers it and how to fix it.
NoSuchElementException: Thrown when FindElement() cannot locate the element before the timeout.
Fix: switch from implicit waits to an explicit WebDriverWait with ExpectedConditions.ElementExists(), and verify the locator in DevTools before using it in code.
StaleElementReferenceException: The element was found, but the DOM refreshed before you interacted with it.
Fix: re-locate the element immediately before each interaction, or wrap the interaction in a retry loop using WebDriverWait.
InvalidSelectorException: The CSS selector or XPath expression is syntactically invalid.
Fix: test the selector in the browser console (document.querySelector() for CSS, $x() for XPath) before putting it in code.
WebDriverException (Chrome not reachable): ChromeDriver version does not match the installed Chrome version.
Fix: update the Selenium.WebDriver.ChromeDriver NuGet package to match your Chrome version, or use WebDriverManager to manage driver binaries automatically.
Start with the NUnit project setup from this tutorial, get the local Selenium C# test passing against the Selenium Playground, then swap in the TestMu AI cloud config to run across browsers and OS combinations without maintaining local drivers.
For the full cloud setup reference, see the TestMu AI Selenium documentation. To run tests across a distributed grid, the Selenium Grid tutorial covers parallel execution in depth.
If you want to validate your Selenium C# knowledge formally, the Selenium C# 101 certification by TestMu AI covers everything in this guide and beyond.
Note: This article was researched and drafted with AI assistance, then reviewed, fact-checked, and published by Andreea, Community Contributor at TestMu AI, whose listed expertise includes Selenium and C#. Every statistic, link, and product claim was verified against primary sources. Read our editorial process and AI use policy for details.
Did you find this page helpful?
More Related Hubs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance