In our previous article, I have explained how to install Selenium for C# in Visual Studio.
In this article, I am going to add chrome driver (an exe file) to execute our Selenium C# Test Automation scripts in chrome browser.
Let’s get started.
Selenium C# – Adding Chrome driver to execute scripts on Chrome Browser
Follow the below steps for executing test automation scripts in Chrome Browser:
1) Download Chrome Driver by following the below steps.
2) Open https://www.seleniumhq.org in any browser say Chrome and click on the ‘Download’ tab as shown below:
3) In the displayed ‘Download’ page, scroll down and click on the ‘Google Chrome Driver’ > ‘latest’ link as shown below:
The following page will be displayed:
Before downloading the Chrome Driver version from the above page, we need to first findout the Chrome Browser version in your machine by following the below steps.
4) Download the chrome driver version that matches with your chrome browser version by following the below steps:
Open Chrome browser and select three dots > Help > About Google Chrome (as shown below)
Observe that the Google Chrome version you are using currently will be displayed as shown below:
As the Chrome Browser version (i.e. Version 75 in our case) should match with the Chrome Driver version (Version 75 should be selected), lets click on the below version:
5) In the displayed page, click on the Windows related link to download as shown below:
6) Extract the Zip file which got downloaded in the previous step as shown below:
7) Observe that a chromedriver.exe file will get extracted from the above ZIP file as shown below:
8) Launch Visual Studio, open the Project in which Selenium is installed (Refer previous article) and right click on the Project in Solution Explorer & select Add > New Folder option as shown below:
9) Provide the name of the folder as anything say ‘Drivers’ as shown below:
10) Now copy and paste the chromedriver.exe downloaded earlier (step 7) into this ‘Drivers’ folder as shown below:
11) Create an object for ChromeDriver predefined Class of Selenium and observe that errors will be displayed as shown below:
ChromeDriver driver = new ChromeDriver("Path to Chrome Driver EXE File");
12) Hover the mouse on ChromeDriver class and select to use OpenQA.Selenium.Chrome; to resolve the above errors as shown below:
13) Observe that the errors got resolved as shown below:
14) Now, copy the path of ChromeDriver EXE file by following the below steps
15) Right click on ‘chromedriver.exe’ file and select ‘Properties’ option as shown below:
16) Copy the path of chromedriver.exe from the Properties pane as shown below:
17) Paste the path into the ChromeDriver() constructor in ChromeDriver object creation statement as shown below:
18) To resolve the error in the above pasted path, add @ and observe that the errors got resolved as shown below:
19) Remove ‘chromedriver.exe’ from the above path and keep the path till \Drivers\ as shown below:
20) Save the Program.cs file and click on ‘Start’ option to execute the Selenium C# Script as shown below:
21) Observe that a blank chrome browser will be displayed as shown below:
The above blank Chrome Browser will be launched when ChromeDriver(“Path of ChromeDriver EXE file”) constructor is executed in our Selenium C# Automation script.
22) In the above code, we can generalize the Chrome Driver Path, instead of giving the complete path of ChromeDriver EXE file from Root Drive as shown below:
ChromeDriver driver = new ChromeDriver(@"../../" + "/Drivers/");
Save and click on the ‘Start’ button to execute the updated statement having generalized ChromeDriver EXE file path and observe that the program will be executed and a blank browser will be launched as expected.
Here conclude our article and we were able to successfully test our first Selenium C# Test Automation Script in Visual Studio.
I will explain more Selenium C# Automation Scripts for performing different operations on the web pages like navigating to the Web Applications, performing click, type & other operations on the UI elements on the Web Application pages in the upcoming articles.
- > To learn more about Selenium C#, continue to the next post (Click on Next Post Link Below)
- > Check complete Selenium C# Tutorial Contents here (Click here)
Please leave your questions/comments/feedback below.
Happy Learning 🙂
On a mission to contribute to the Software Testing Community in all possible ways.