Getting Started with WebDriver C# in 10 Minutes

Getting Started with WebDriver C# in 10 Minutes

WebDriver is a tool for automating testing web applications, and, in particular, to verify that they work as expected. It aims to provide a friendly API that’s easy to explore and understand, which will help make your tests easier to read and maintain. It’s not tied to any particular test framework so that it can be used equally well with MSTest, NUnit, TestNG, JUnit and so on. This “Getting Started” guide introduces you to WebDriver’s C# API and helps get you started becoming familiar with it.

Create Your First WebDriver Test Project

1. Create New Test Project in Visual Studio.
WebDriver Test Project
2. Install NuGet package manager and navigate to it.
Open NuGet Manage Console
3. Search for Selenium and install the first item in the result list
Install Selenium WebDriver Package

Code Examples In WebDriver

Create an instance of a driver.

IWebDriver driverOne = new FirefoxDriver();
IWebDriver driverTwo = new InternetExlorerDriver(C:PathToMyIeDriverBinaries);

Only the FirefoxDriver can be created without parameters. For all other drivers you need to point the location where the specific driver is downloaded.

Аdditional steps are required to use Chrome Driver, Opera Driver, Android Driver and iPhone Driver
Navigation to specific URL.


Locating Elements with WebDriver

  • By ID
IWebElement element = driverOne.FindElement(By.Id(myUniqueId));
  • By Class (Find more than one element on the page)
IList<IWebElement> elements = driverOne.FindElements(By.ClassName(green));
  • By Tag Name
IWebElement frame = driverOne.FindElement(By.TagName(iframe));
  • By Name
IWebElement cheese = driverOne.FindElement(By.Name(goran));
  • By Link Text
IWebElement link = driverOne.FindElement(By.LinkText(best features));
  • By XPath
IList<IWebElement> inputs = driverOne.FindElements(By.XPath(//input));
  • By CSS Selector
IWebElement css = driverOne.FindElement(By.CssSelector(#green span.dairy.baged));

Chaining Locators

Use a Chain of Locators in order to find a specific element.

IWebElement hardToFind = this.driverOne.FindElement(By.ClassName(“firstElementTable)).FindElement(By.Id(“secondElement));

 IWebElement Methods

IWebElement Methods

IWebElement Properties

IWebElement Properties

HTML Element Actions in WebDriver

Type Text into a field using Selenium WebDriver SendKeys() function.

IWebElement element = driverOne.FindElement(By.Name(search));
element.SendKeys(Automate The Planet!);

Select Drop Down Value. First you need to add NuGet Package to your project- Selenium.Support.

SelectElement selectElement = new SelectElement(driver.FindElement(By.XPath(/html/body/select)));

You can find a lot more code and examples in the official WebDriver C# Documentation


  • Diomedes Domínguez

    Is there a way to simulate a vertical scroll in the website?

    • Hi Diomedes,

      Thank you for your question. You can try the following options:

      WebDriver driver = new FirefoxDriver();
      JavascriptExecutor jse = (JavascriptExecutor)driver;
      jse.executeScript(“window.scrollBy(0,250)”, “”);


      Robot robot = new Robot();

      • Diomedes Domínguez

        In which nugget package is Robot ?

    I want to set the user agent for the Firefox web browser, but I’m getting this error message, this is the code:

    var profile = new FirefoxProfile();
    “Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Nokia; Lumia 520) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/13.10570”);

    driver = new FirefoxDriver(profile);

    I tried adding this block, but I have the same result.
    profile.SetPreference(“network.proxy.type”, 1);
    profile.SetPreference(“network.proxy.socks”, “”);
    profile.SetPreference(“network.proxy.socks_port”, 7055);

  • Pingback: Strategies for Selenium UI Testing - Innovative Teams()