Hunt down knowledge with our help

FAQ about Selenium WebDriver on c# with examples

Selenium is a set of tools that are used to automate web browser work. It is possible to use these tools with such popular languages as C#, PHP and Java.

How to install Selenium tools to the project in Visual Studio?

First of all, it is necessary to install Selenium.Webdriver packages to the project. By means of NuGet package manager programmer can install them right from the project window:


  1. Go to Tools/NuGet Package Manager/Manage NuGet Packages for Solution…/ in Visual Studio
  2. In “NuGet – Solution” tab go to Browse
  3. In search line write the package name – Selenium.Webdriver
  4. Choose the package and press install button

Secondly, it is necessary to download drivers that match browsers you choose to test and OS of the computer:

  1. Update all browsers you want to test
  2. Download appropriate driver
  3. Place downloaded driver (.exe) to the application directory

Finally, using directives to Selenium namespaces must be added:

   using OpenQA.Selenium;
   using OpenQA.Selenium.Chrome;
   using OpenQA.Selenium.Firefox;

Note: Webdriver won`t work with Firefox browser if Firefox: Developer Edition is not installed.

How to create an instance of WebDriver on c#?

There are some classes for each webdriver:

  • ChromeDriver
  • FirefoxDriver
  • and so on

Also, all of them inherit IWebDriver interface. An instance of webdriver class can be initialized by means of default constructor (that has some overloads):

   FirefoxDriver firefox = new FirefoxDriver();
   ChromeDriver chrome = new ChromeDriver();
   List<IWebDriver> drivers = new List<IWebDriver>() { firefox, chrome };  //put webdrivers to the list

How to force webdriver to wait until the web content is fully loaded?

Working with webdriver, programmer can stumble upon such a problem that webdriver instance won`t wait until web page content is fully loaded. That can lead to exceptions when command to find and perform some actions with dom element is executed. To solve this issue Implicit wait time should be set.

   IWebDriver driver = new ChromeDriver();
   driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); //driver will try to execute dom commands for 10 seconds in case of failure

How to hide command prompt (console) while using webdriver on c#?

   ChromeDriverService chromeservice = ChromeDriverService.CreateDefaultService();
   chromeservice.HideCommandPromptWindow = true; //hide console window
   ChromeDriver driver = new ChromeDriver(chromeservice);

   FirefoxDriverService firefoxservice = FirefoxDriverService.CreateDefaultService();
   firefoxservice.HideCommandPromptWindow = true; //hide console window
   FirefoxDriver driver = new FirefoxDriver(firefoxservice);

How to execute your javascript code in webdriver window?

There are two similar options to inject and execute new javascript code in webdriver:

  • by means of webdriver class method ExecuteScript or ExecuteScriptAsync
  • by means of IJavaScriptExecutor interface that is inherited by webdriver classes
    FirefoxDriver firefoxdriver = new FirefoxDriver();
    firefoxdriver.ExecuteScript("alert("Hello!");");  //your js code

    IWebDriver driver= new FirefoxDriver();
    IJavaScriptExecutor executor = driver as IJavaScriptExecutor;
    executor.ExecuteScript("alert("Hi!");");   //your js code

How to move cursor to some point in webdriver window?

In case of web pages what contain actions associated with mouse moves, ability to manipulate mouse is needed. For example, in order to parse web pages, you will need to get whole usefull html content from web sites. As a rule, for a number of reasons (often for protecting from spam bots) web masters try to hide some part or a whole sites` content from beeing loaded by bots (in our case by webdriver bot). The most popular way of achieving such kind of a protection is to check if the mouse is moved while surfing the site. Sometimes the folowing code can be really helpful:

   ChromeDriver driver = new ChromeDriver();
   Actions action = new Actions(driver);

   action.MoveByOffset(5,5).Perform();  //moves cursor to point (5,5)

   action.MoveByOffset(10, 15).Perform();  //moves cursor to point (10,15)

How to navigate to some webpage in webdriver window?


How set page load time?

   driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(10);  //time to wait untill page is loaded
      driver.Navigate().GoToUrl(url);  //if page is not loaded in 10 sec exception is thrown
      //catch timeout exception

How to set Chrome command line switches?

In case of making complex and various tests chrome CLI switches can be really useful and sometimes required.

   ChromeOptions options = new ChromeOptions();
   options.AddArguments(new List<string>() { "switch 1", "switch 2", "and so on" });  //adding list of chrome CLI arguments

   ChromeDriver driver = new ChromeDriver(options);

Full list of CLI agruments.

How to set windowless regime in webdriver?

If tests are done without requirement of showing up any useful information it is logical to switch off window mode. To run webdriver without window, add neseccary CLI arguments:

   ChromeOptions options = new ChromeOptions();
   options.AddArguments(new List<string>() { "headless"});


Related Posts