Selenium Webdriver installation using c#| EngineerDiaries

Jan 14, 2021, 1452 Views, 0

1.The objective of the blog: 

A basic introduction to Selenium Webdriver, asp .net technology, and the concept of web drivers. Then, will create a new project in the visual studio, add libraries to it. Add web drivers to the system, in this particular example will install chromedriver. using installed libraries and NuGet package will access the chrome via chromedriver and will test a URL on the chrome. The exact sample of it is given below in the following diagram. The steps involved in the installation process are as:

  1. Installing drivers for browser in the system, here we are adding chromebrowser
  2. Creating an MVC project in dot net and visual studio 2017
  3. Adding Selenium.Webdriver NuGet package
  4. Access chromedriver to access site URL

Selenium Webdriver installation using c#| EngineerDiaries

2.What is Selenium Webdriver?

Selenium testing tool is an automation testing specifically for the web application to test the application on cross-browsers. What it means is that the feature you are developing can be tested on multiple browsers by generating scripts hence, you have automated the testing process. To be a bit more specific, you test the performance and UI/UX of the application. Now Selenium testing offers three tools: Selenium Webdriver, Selenium Grid, and Selenium IDE. Selenium Webdriver helps to test the application using drivers for browsers which means is that you write the application of the programming language of the choice, then add selenium webdriver libraries then write the script using them and test the performance on cross-browsers via browser-specific drivers. The below image makes it clearer.

Selenium Webdriver installation using c#| EngineerDiaries

3.Working with and C#

If you have worked with C, C++, Java, or any object-oriented language ever, then that's all you need here. Also, software testing and writing scripts involve the basis of if-else and loops only. Asp dot is a framework for building applications in C#. Here, we will be creating a project MVC project, but of course, you can create an empty project as well. We are using Visual Studio 2017, you can also use Visual Studio 2019. We are using dot net core 2.2, again you can downgrade or upgrade accordingly but it's always better to use the latest stable version.

4.Installing Chromedriver for Windows

Before Selenium Webdrivers, there was Selenium Remote Control where  RC was a proxy server, so the browser used to send requests to the proxy server first where then the output was tested on the browser. You can just imagine the nature of complicated architecture here. But with Selenium Webdriver, the browsers were controlled at the operating system level itself using the drivers. Multiple browsers are supported by Selenium Webdriver like Chrome, Firefox, Edge, Internet Explorer, Opera, Safari, but this example will work with chromedriver.

1.Check the version of google chrome.

  Help->About Google Chrome-> Check the version

Selenium Webdriver installation using c#| EngineerDiaries

2.Download the chrome driver from the chrome driver

3.Create a folder in the system and access the drivers from there

Selenium Webdriver installation using c#| EngineerDiaries


4.If you get a screen like this then the driver is just working fine.

Selenium Webdriver installation using c#| EngineerDiaries

5. Setting up Visual Studio and adding Selenium webdriver NuGet package manager.

Here we are adding dot net core 2.2 MVC project in Visual Studio 2017 followed by Selenium Webdriver NuGet package manager. You need not make an MVC project only, can definitely use your choice of web application it would be the same. Here the code also is being added to the index method only to keep the focus on logic than architecture.

Selenium Webdriver installation using c#| EngineerDiaries

Selenium Webdriver installation using c#| EngineerDiaries

6. Code to access URL using webdriver

using WebApplication1.Models; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Remote; using OpenQA.Selenium.Support.UI; namespace WebApplication1.Controllers { public class HomeController : Controller { public IActionResult Index() { ChromeOptions options = new ChromeOptions(); options.BinaryLocation="C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"; DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.SetCapability(ChromeOptions.Capability, options); ChromeDriver driver = new ChromeDriver("C:\\WebDriver\\bin", options); driver.Navigate().GoToUrl(@"");
On debugging, If the code launches the browser chrome and then navigate to the URL. Then you have successfully done with Selenium Webdriver installation using C#.Selenium Webdriver installation using c#| EngineerDiaries

Code Summary:

1.Binary locations are in fact optional if your visual studio is unable to access chrome. Which was the error in our case. So, add the location of chrome.exe in your system. 

2.Main code starts from chromeDriver as above we were setting options to access chrome only. The first parameter we give the path to the chromedriver which we installed in the previous step. If you debug it and reach this point and cmd opens then the driver is working just fine.

3.driver.navigate() to access the webpage or website for which you want to automate.


You have successfully added chromeDriver to your windows system and your c# code is able to access your site through Selenium Webdriver using c#. The next step is to write scripts to automate cross-browser web application testing.

Comments Section: