selenium-webdriver Basic Selenium Webdriver Program C#


Example

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

namespace BasicWebdriver
{
    class WebDriverTest
    {
        static void Main()
        {
            using (var driver = new ChromeDriver())
            {
                driver.Navigate().GoToUrl("http://www.google.com");
            }
        }
    }
}

The above 'program' will navigate to the Google homepage, and then close down the browser after fully loading the page.

using (var driver = new ChromeDriver())

This instantiates a new WebDriver object using the IWebdriver interface and creates a new browser window instance. In this example we are using ChromeDriver (though this could be replaced by the appropriate driver for whichever browser we wanted to use). We are wrapping this with a using statement, because IWebDriver implements IDisposable, thus not needing to explicitly type in driver.Quit();.

In case you haven't downloaded your WebDriver using NuGet, then you will need to pass an argument in the form of a path to the directory where the driver itself "chromedriver.exe" is located.


Navigating

driver.Navigate().GoToUrl("http://www.google.com");

and

driver.Url = "http://www.google.com";

Both of these lines do the same thing. They instruct the driver to navigate to a specific URL, and to wait until the page is loaded before it moves to the next statement.

There are other methods tied to navigation such as Back(), Forward() or Refresh().


After that, the using block safely quits, and disposes the object.