Selenium C# Code For Login Page

How To Articles

Recently, I had the opportunity to work on a project that required me to automate a login page using Selenium and C#. As a software developer, I find it incredibly satisfying to automate repetitive tasks, and login pages are a perfect example of that. In this article, I will walk you through the process of writing Selenium C# code for automating a login page and share some personal insights along the way.

First, let’s talk about Selenium. Selenium is a powerful open-source tool widely used for automating web browsers. It provides a rich set of libraries and APIs that allow developers to interact with web elements, simulate user actions, and perform various testing and automation tasks. In our case, we will be using Selenium WebDriver, which enables us to control a web browser programmatically.

Before diving into the code, let’s clarify our goal. The objective is to automate the login process of a web application using Selenium with C#. We want our code to open the login page, enter the username and password, and click the login button. Sounds simple enough, right?

Let’s start by setting up the Selenium WebDriver in our C# project. First, we need to install the Selenium WebDriver NuGet package. Open Visual Studio and create a new C# project. Right-click on the project in the Solution Explorer, select “Manage NuGet Packages,” and search for “Selenium.WebDriver.” Click on “Install” to add the package to our project.

Once the WebDriver package is installed, we can start writing our code. Open the program.cs file in your project and add the following using statements at the top:

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

Next, let’s create an instance of the ChromeDriver, which is a WebDriver implementation for Google Chrome browser:

IWebDriver driver = new ChromeDriver();

Now that we have our WebDriver instance, we can navigate to the login page using the driver’s Navigate() method:


Replace “” with the actual URL of your login page. This line of code tells the WebDriver to open the specified URL.

Next, we need to locate the username and password fields on the login page and enter the credentials. We can do this by using the driver’s FindElement() method and its various locating mechanisms, such as XPath, ID, or CSS selector. For example, if the username field has an ID of “username” and the password field has an ID of “password”, we can locate them as follows:

IWebElement usernameField = driver.FindElement(By.Id("username"));
IWebElement passwordField = driver.FindElement(By.Id("password"));

Once we have located the fields, we can use the SendKeys() method to enter the username and password:


Replace “myusername” and “mypassword” with your actual credentials.

Finally, we can locate the login button and click it using the Click() method:

IWebElement loginButton = driver.FindElement(By.Id("login-button"));

That’s it! We have successfully automated the login process using Selenium and C#. Now, every time we run our code, it will open the login page, enter the credentials, and click the login button.


Automating a login page using Selenium and C# can be a valuable time-saving technique for software developers. In this article, we explored the process of writing Selenium C# code to automate a login page. We learned how to set up the Selenium WebDriver, navigate to the login page, locate and interact with the username and password fields, and click the login button. By automating repetitive tasks like login processes, we can focus on more complex and interesting aspects of software development. So go ahead, give it a try, and see how automation can enhance your development workflow!