Webdriver is an application programming interface for Selenium. It gives us the tools to send commands to be executed by Selenium. Thanks to WebDriver we can let Selenium know that we want our browser to navigate to a particular URL or that we want to press a button or find a Element (such as a textbox) on a page.
In the previous post you can see how to install Visual Studio and Create your first project. Once you have created your project we just need to use the Webdriver Application Programming Interface (API) in order to have control over our webbrowser. To do this, just take the next steps.
Right click on your project and select “Manage NuGet Packages”.
Next, select “Browse” in the top left corner and type “Selenium” in the search box. “Selenium.Webdriver” should popup on top. Select the latest stable version (in my case this is 3.141.0) and click “Install”.
Then we just need to decide what browser we will use. In this tutorial we will use Google Chrome. For this we just need to install ChromeDriver so that we can actually use Webdriver for Chrome. Install the latest version (in my case this is 75.0.3770.140)
We are almost finished. Go back to your project on the right hand side of Visual Studio and double click Program.cs.
What we want to do now, is start Chrome and navigate to the webpage of our choice. First thing we need is to reference Selenium and Chrome from our code so that Visual Studio knows what we are talking about. Add the following code to the top of “Program.cs”.
using OpenQA.Selenium; using OpenQA.Selenium.Chrome;
In our “Main” method we will now create an instance of Webdriver (ChromeDriver to be more specific) and set the startup page to “http://www.mashitupide.be/TestPages/TestPage1.html”
IWebDriver driver = new ChromeDriver(); driver.Url = "http://www.mashitupide.be/TestPages/TestPage1.html";
As an alternative you could also just instantiate WebDriver and explicitly navigate towards a URL. This is especially usefull if you need to navigate multiple times during the execution of your C sharp code.