Setup Selenium 4 with C Sharp (C#) in Visual Studio
What you will learn:
Download & Install visual studio
Create visual studio project
Understanding project components
Adding Selenium Webdriver and Chrome browser dependencies
Create First C Sharp script using Selenium 4
Download & Install visual studio
Download community edition of Visual studio (open source, free of cost) from https://visualstudio.microsoft.com/downloads/
Expand Visual Studio 2022 and download community edition
Download the exe and install Visual studio.
During the installation you might see below window. Select ‘.NET desktop development’ and ‘Universal Windows Platform development’ packages
After installation
(Optional step) After installation, you can sign in with your outlook account. If you do not have microsoft account, you can create it by going to the website https://signup.live.com/signup.aspx
You can click ‘Not now, maybe later’
Select theme and click ‘Start Visual Studio’
Click ‘Create a new project’
Select ‘Console App’ and click Next.
To download older community versions of VS
Click ‘Older Downloads’
Expand 2019
Click Download
Signin with your outlook account and download the community edition
For 2019 comminty edition, when you are creating a new project, select .NET 5.0 (Current)
Create visual studio project (snapshots taken from visual studio 2017 version)
Launch Visual studio and click File > New >Project
Select ‘Visual C#’ on the left menu and the type of project (application) as ‘Console App (.NET Framework)’
Browse & select the desired location where you would like to save your project.
Type application name ‘SeleniumCSharpAppApplication’
Click OK.
The ‘Solution Explorer’ window will come up giving details of your project (View >Solution explorer). The solution explorer shows the name of the project you have just created
Understanding project components
By default 2 folders (Properties, References), 1 config file and 1 class file gets created inside this project (see above).
The dlls get added inside the References folder
Program.cs is a C sharp class file having .cs extention
Adding Selenium Webdriver and Chrome browser dependencies
Right click Refrerences > Manage NuGet Packages…
Click ‘Browse’ tab and search for ‘selenium’. Notice below that we have Selenium version 4.1 available
Install Selenium.WebDriver
Install Selenium.Support
Install Selenium.WebDriver.Chrome
These will than appear under ‘Installed’ section
You should see these references under ‘References’
Create First C Sharp script using Selenium 4
Let us create an instance of chrome driver
Save and click ‘Start’. Notice below that a blank chrome browser has been launched
Let us navigate to a site
Notice below that the site is launched
Code Snippet
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeleniumCSharpAppApplication
{
class Program
{
static void Main(string[] args)
{
IWebDriver dr = new ChromeDriver();
dr.Navigate().GoToUrl("https://www.way2automation.com/");
}
}
}
Thank you for reading!