Selenium WebDriver – Using Chrome WebDriver in Visual Studio C#

PerfectoTopAd

Configuring Selenium 2.0 with Chrome to work in Visual Studio

In a previous post/video I showed how to get started using Selenium WebDriver for IE in Visual Studio.

Since then I've received a few questions on how to do the same with Selenium Web Driver and Chrome in Visual Studio. Rather than answer each one individual, I thought it would be better to frame my replay in the form of a quick post.

Today in this post we are going to check out how to configure Selenium 2.0 WebDriver to work with Visual Studio and Google Chrome.

First Download the Selenium C# Client Drivers

  • The first thing we want to do is download the C# DLLs from Selenium's web site:

http://seleniumhq.org/download/


  • You also need to download the chromedriver from:


http://code.google.com/p/chromedriver/downloads/list:

  • Once you've downloaded the required Selenium files extract the zips to a local drive on your computer.

Configure Visual Studio to work with Selenium

  • Launch Visual Studio and start a new project
  • Select ‘Console Application' and name your project mySelenium


  • In your project select Project>Add References from the tool bar and browser to the location where you extracting the DLLs to. Make sure that you select the correct version of .NET that you will be using:


  • Select all the DLLs and click ok


    Time to Code!

    Cool – now that we have the Selenium DLLs referenced in our project it time to test the configuration by running a quick test.

    1. At the top of your project code after the last ‘using' namespace add the following Selenium namespaces:

    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.UI;
    

    2. Add the following code in your static void main section:

    //!Make sure to add the path to where you extracting the chromedriver.exe:
    IWebDriver  driver = new ChromeDriver(@"D:\Download\chromedriver"); //<-Add your path
    driver.Navigate().GoToUrl(https://joemainzone-ilyznmosqlw0zs.netdna-ssl.com/HpSupport.html);
    

    Note: You could also create an environment variable named webdriver.chrome.driver on your machine that's value is the path to where the local chromedriver.exe is located. If you setup an webdriver.chrome.driver variable you would not have to pass the chromedriver argument when you create a ChromeDriver instance.

    Your code should look like this:


    3. Run the test – Google Chrome should start and bring up https://www.joecolantonio.com/HpSupport.html


    Chrome Developer Tools

    If you are going to be scripting against Chrome you should check out the built in Developer tools (Ctrl+Shift+I). For those familiar with QTP this is like QTP's spy feature.

    1. In Google Chrome go to Tools>Developer tools

    2. Using the Chrome magnifying glass icon feature allows you to spy on an element's attributes. You then use these attribute in your Selenium code to help identify fields in you web application.


    3. For example if we want to navigate to www.joecolantonio.com/HpSupport.html and select a value from the ‘Select your tool & Version' you would point the magnifying glass to the ‘Select you tool & version” field to get its attributes:


    4. Now that we know what the id is we can use that to help Selenium interact with it. For example:

    IWebDriver  driver = new ChromeDriver(@"D:\Download\chromedriver");       driver.Navigate().GoToUrl(https://joemainzone-ilyznmosqlw0zs.netdna-ssl.com/HpSupport.html); 
    IWebElement myField = driver.FindElement(By.Id("tools"));      
    myField.SendKeys("QTP10");
    

    5. Run the test – it should start Chrome, navigate to http://www.joecolantonio/HPSupport.Html and select QTP10 from the Select your tool & version.

    Good Luck and happy Selenium scripting to you!


     

17 comments
vivek sharma - September 2, 2013

Hello ,
Thanx for beautiful tutorial,
I wud like to ask u a question ..

I want to highlight those controls ( text box, buttons ) which are being triggered while selenium automation process.

I am using VS 2010 + selenium

Reply
shari - January 22, 2014

I’m new to Visual Studio and C# but have used Selenium Webdriver with Java and Eclipse. When I run the code (in Visual Studio) to get to your page with Chrome or IE, I get an error that says the driver does not exist (then a link to where it can be downloaded). I tried extracting it into the .net folder but still no luck. Any thoughts?

Reply
    Joe Colantonio - January 24, 2014

    HI Shari – you need to download the IE or Chrome driver servers and add their paths to you path environment variable. That should resolve this issue.

    Reply
Nabhanya - March 30, 2014

Great.. a step by step…
Thank you for sharing.
The below link also shares the information on selenium with java and discuss more in terms of practical working examples to build a framework
http://seleniumeasy.com

Reply
seema - July 24, 2014

can i know selenium 2.39 version will support which are the versions of chrome browser….
please reply me on urgent basis

Reply
Nir Selickter - September 23, 2014

Hi,

Thanks for this simple working tutorial.

Now i have the next question….

we have some hw device that we manage by browser, i open chrome and browse to my device “http://192.168.15.248”
and i see the ‘index.html’ file with button, text box, and other GUI
component.

Now i want to do some change – i want to do the gui in WPF and instead of html and invoke the JS functions from C#.

How can i do it ?
( here exist what i want but in WinForm and with IE)
http://www.codeproject.com/Tips/127356/Calling-JavaScript-function-from-WinForms-and-vice

Many Thanks

Nir

Reply
Tali - October 21, 2014

Is there an option to open a new tab for the opened chrome browser window?

Reply
wasif Saeed - March 8, 2015

Hi Dear i am using selenium with chrome driver and i am facing a problem. The problem is that when i click on a button on page the page run some client side code (ajax call) and load a table in its body the table size is big that`s why its take time to load. i mean while a got exception of element to find the code comment bellow
Please help me Thanks in Advance :)

ChromeDriver driver = new ChromeDriver(chrome_path);
driver.Navigate().GoToUrl(@url);
var submitButton = driver.FindElementByXPath(“//input[@value=’Submit’]”);

submitButton.Click();
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(15));
var element = wait.Until( dr => dr.FindElement(By.XPath(“//*[@id=’appjudgment’]/div/b”)));
i got exception on last line

Reply
Sufiyan - December 11, 2015

this post is awesome for beginners,,, thanks

Reply
Introduction to Selenium WebDriver – Selenium Tutorial | selenium webdriver tutorial - May 24, 2016

[…] While at present aware, tests might be composed in almost any software vocabulary backed by Selenium for example Java, C++, Python, ruby, perl etc. Still about 80% of the trainers are relying on java. Because the Selenium Webdriver alone is created in java, there are many more advantages in applying java while the method terminology. Therefore, some online Selenium tests classes will provide course on Java also in the beginning of the class. https://www.joecolantonio.com/2013/01/18/selenium-webdriver-using-chrome-webdriver-in-visual-studio-c… […]

Reply
annette - August 24, 2016

When I try this I am getting an exception. I think its not catching the chromedriver. What am I doing wrong?

Reply
Gabrielle - August 30, 2016

This is really helpful, sir! Thanks! Otw to awesomeness <3

Reply
Tarun Sahani - April 12, 2017

It is very nice article for beginners. Thanks Man.

Reply
Click here to add a comment

Leave a comment: