Web automation is now a cornerstone in software testing and task scripting, allowing developers and testers to simulate user behavior, perform regression testing, and automate repetitive actions. One of the most powerful tools for browser automation is Selenium, and with the rise of cross-platform development, many are now using C# in Visual Studio Code (VS Code) to build these solutions.
In this blog, we’ll walk through how to set up and use Selenium with C# in VS Code, and execute browser commands programmatically using Cursor
to mimic user interactions.
Why Selenium with C#?
Selenium supports multiple programming languages, but C# offers robust object-oriented support, great performance, and a rich ecosystem with .NET. It’s a preferred choice in enterprises and QA teams familiar with Microsoft technologies.
Benefits of using C# with Selenium:
- Strong typing and compile-time checks
- Easy integration with NUnit/XUnit for testing
- Full .NET Core compatibility for cross-platform automation
- Rich LINQ support for manipulating test data
Prerequisites
Before jumping into code, ensure you have the following installed:
- .NET 6 SDK or higher
- Visual Studio Code
- C# extension for VS Code
- [Google Chrome or Firefox] (for running automated tests)
- ChromeDriver or GeckoDriver (depending on browser choice)
- NuGet CLI or VS Code’s built-in package manager
Step-by-Step Setup
1. Create a New Console App
dotnet new console -n SeleniumAutomation
cd SeleniumAutomation
2. Add Selenium WebDriver NuGet Packages
Use the following commands to install necessary libraries:
dotnet add package Selenium.WebDriver
dotnet add package Selenium.WebDriver.ChromeDriver
dotnet add package Selenium.Support
3. Code: Launching and Controlling Browser
Open the Program.cs
file and write the following code:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Threading;
class Program
{
static void Main(string[] args)
{
ChromeOptions options = new ChromeOptions();
options.AddArgument("--start-maximized"); // Open browser in maximized mode
using IWebDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://www.google.com");
Thread.Sleep(2000); // Wait for 2 seconds
IWebElement searchBox = driver.FindElement(By.Name("q"));
searchBox.SendKeys("Selenium WebDriver with C#");
searchBox.SendKeys(Keys.Enter);
Thread.Sleep(4000); // View results
driver.Quit(); // Close browser
}
}
Using Cursor Commands to Simulate Human-Like Actions
For more realistic user interaction, we can control the cursor or mouse pointer using external libraries like InputSimulator
or native Windows API wrappers. Here’s an example using the System.Windows.Forms.Cursor for simple movement (only works on Windows):
Add Reference
dotnet add package System.Windows.Forms
Simulate Cursor Movement
using System.Windows.Forms;
using System.Drawing;
class CursorDemo
{
public static void MoveCursor()
{
// Move the cursor to a specific location (x:100, y:200)
Cursor.Position = new Point(100, 200);
Console.WriteLine("Cursor moved to (100, 200)");
}
}
Automating Clicks Using JavaScript with Selenium
You can also execute mouse-like interactions via JavaScript when real cursor movement isn’t necessary:
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("document.querySelector('input[name=q]').click();");
Running and Debugging in VS Code
To run the project in VS Code:
- Open your project folder in VS Code.
- Press
Ctrl + Shift + P
, select .NET: Generate Assets for Build and Debug. - Press
F5
to build and run the application. - The browser should launch and perform the automated actions.
Tips for Effective Browser Automation
- Always wait for elements to be visible using
WebDriverWait
orExpectedConditions
. - Handle exceptions to prevent hanging browsers.
- Use headless mode for CI/CD environments:
options.AddArgument("--headless");
- Log your test steps using
Console.WriteLine()
or integrate with logging frameworks.
Conclusion
Automating browsers with Selenium and C# in VS Code is a powerful way to streamline testing and repetitive tasks. With cursor control, you can simulate real user actions and make your automation scripts more interactive. Whether you’re automating form submissions, scraping data, or testing web apps, this setup gives you all the flexibility you need in a lightweight and efficient environment.