Selenium 2.0 WebDriver with Visual Studio, C#, & IE – Getting Started

by Joe Colantonio on July 31, 2012

Post image for Selenium 2.0 WebDriver with Visual Studio, C#, & IE – Getting Started

Issues Configuring Selenium WebDriver to work in Visual Studio

In this post I'll show you how to configure Selenium 2.0 WebDriver to work with Visual Studio and Internet Explorer. (For some reason, I initially had a hard time getting this configured correctly.)

After much googling and trying different solutions from various places, I finally got Selenium work. Hopefully this post will help you avoid some of the issues I ran in to.

Download Selenium C# Client Drivers

  • The first step is to download the C# dlls from Selenium's web site.
  • You'll also want to download the Internet Explorer Driver Server:


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

FYI - the Selenium Web Drivers work against IE 6,7,8 and 9 both 32-bit and 64-bit versions.

Configure Visual Studio to work with Selenium

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


  • Next select Project>Add References from the tool bar and browser to the location where you extracted the Selenium dlls to. (Also be sure to select the correct version of .NET)


  • Select all the dlls and click ok


    Time to Code!

    Cool!! Now that we have the Selenium dlls referenced in our project, it's time to test the configuration by running a quick test.

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

    using OpenQA.Selenium;
    using OpenQA.Selenium.IE;
    using OpenQA.Selenium.Support.UI;
    

    Add the following code in your static void main section:

    //!Make sure to add the path to where you extracting the IEDriverServer.exe:

    IWebDriver driver = new InternetExplorerDriver(@"C:\Download\IEDriverServer");
    driver.Navigate().GoToUrl("http://www.joecolantonio.com/HpSupport.html");
    

    Your code should look like this:


    Run the test. joecolantonio.com should appear.

    IE Developer Toolbar

    If you are going to be scripting against IE you'll want to install the Internet Explorer Developer Toolbar. (Those familiar with QTP are aware that this is similar QTP's spy tool)

    Using the IE Dev's Arrow feature allows you to spy on an element's attributes. You can then use those attributes in your Selenium code to help identify fields in you web application.


    For example if we want to navigate to www.joecolantonio.com/HpSupport.html and select a value from the 'Select your tool & Version' we would point the IE Dev arrow to the field to see its attributes:


    Now that we know what the Id is, we can use that information to help Selenium interact with it. For example:

    myField = new driver.FindElement(By.Id("tools"));
    myField.SendKeys("QTP10");
    

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

    WTF's to look out for

    If you getting the following error:

    "InvalidOperationException was unhandled – unexpected error launching Internet Explorer. Protected Mode setting are not the same for all zones.

    Enable Protected Mode must be set to the same value (enabled or disabled) for all zones"


    In IE tools>Internet Options>Security tab, make sure all your zones are set to the same value:


If you are getting the following error:

"DriverServiceNotFoundException was unhandled: The file … \IEDriverServer.exe does not exist. The driver can be downloaded at"


Make sure that you downloaded the IEDriverServer.exe and also that you have the correct path specified in your code

IWebDriver driver = new InternetExplorerDriver(@"C:\YourPath\IEDriverServer"); //<-Add your path

Good Luck, and happy Selenium scripting!

 


 

{ 27 comments… read them below or add one }

Hardik Bhatt November 27, 2012 at 6:00 am

Thanks a million mate. wasnt able to find any tutorial small and precise to get it working for chrome. this IE implementation did the job for me. thanks again mate. cheers.

Reply

Joe Colantonio November 27, 2012 at 8:26 pm

Hardik Bhatt » Awesome – glad it helped!

Reply

Anurag Jain December 19, 2012 at 11:51 am

This tutorial helps a lot and make me able to start scripting sucessfully.

Reply

Joe Colantonio December 20, 2012 at 10:45 pm

Anurag Jain » Awesome – that’s what I like to hear!

Reply

Engy January 17, 2013 at 2:55 pm

hi joe thanks for the tutorial .actually i need your help ,i’m using selenium webdriver with visual studio c# chrome driver ,i added the selenium references and installed the chrome driver but i cannot add it as reference.

Reply

Joe Colantonio January 18, 2013 at 1:29 pm

Engy » Hi are you getting an error message? I just verified that it worked for me. Did you verify that in your project>references that you have all the selenium dlls added? Also make sure that you added the name space namespace OpenQA.Selenium.Chrome. For the chrome driver you need to either have the path to it in your machines PATH environment variable or add it as an argument when you instantiate the Chrome Driver:

IWebDriver driver = new ChromeDriver(@”C:\pathToTheDriver\chromedriver\”);

Reply

Luay Zakaria January 24, 2013 at 7:33 pm

Awesome tutorial.. thx m8
this got me up and running in no time.. would’ve taken me hours otherwise!

Reply

Joe Colantonio January 25, 2013 at 12:59 pm

Cool – glad it helped. Let me know of any other Selenium issues you would like to see post on. Cheers~Joe

Reply

jay February 26, 2013 at 9:36 pm

I was new to Selenium but this tutorial made me hit the ground running in 15 minutes!~
Good job Joe!

thanks
Jay

Reply

Joe Colantonio February 27, 2013 at 10:32 pm

jay » Cool – thanks Jay! Glad it helped you.

Reply

Saravanan Babhu February 27, 2013 at 6:27 am

Awesome tutorial. Super like

Is it the same step I have to follow for safari browser in windows 7 machine.

Reply

Joe Colantonio February 27, 2013 at 10:31 pm

Saravanan Babhu » Thanks Saravanan – I have not tried against Safari yet.

Reply

Khairil Anuar March 14, 2013 at 3:33 am

Thanks so much Joe. Simple and it’s work.. Awesome!!
Now I can start the scripting with IE.

Thanks again.. :)

Reply

Joe Colantonio March 14, 2013 at 1:40 pm

Khairil Anuar » Thanks for the positive feedback – I really appreciate it! Glad it helped!

Reply

Diptangshu Santra March 24, 2013 at 9:34 am

Hi Joe. Thanks for this page.
I am a QTP prof. I m completely new to Selenium. I am good with C#, but not that much with Java.
I find, that most of Selenium frameworks, like TestNG and ReportNG, are aligned to Java. But could not find as such for C#.
People say, NUnit and MBUnit aren’t that handy. Also, no reporting framework is available.
Do I need to learn Java all from scratch, or there are ways in C#? Please suggest. Thanks in advance !

Reply

Joe Colantonio May 3, 2013 at 4:02 pm

Diptangshu Santra » Not sure what detail you need but we use nNunit without any issue. What do you need to do that nUnit is not handling? thanks~joe

Reply

Dilshan April 2, 2013 at 7:27 am

Thanks for the info Joe. Worked like a charm.

Reply

shakkeer April 3, 2013 at 7:33 am

Thanks a lot simple and perfect

Reply

Joe Colantonio April 4, 2013 at 10:50 am

shakkeer » Excellent!

Reply

sana April 8, 2013 at 6:33 am

please help me its not working as i have done all you guided …
issue is that when i am running the code iside visual studio it give an error that “visual studio can not start dbugging because the debug target is missing. please buid the project and retry, or set the outpput path and assembly name properties appropriately to point on correct location for target assembly” how to fix it??

Reply

Joe Colantonio April 9, 2013 at 5:05 pm

sana » This is usually solved by changing the startup object in your project. This Microsoft thread has some suggestions for how to fix this error:
http://social.msdn.microsoft.com/Forums/en-US/vsdebug/thread/1a89d2df-0c57-4bb2-8c1a-b619e6cbb065/

Reply

rony April 18, 2013 at 1:50 pm

Great sample.

Is there a way to record on IE using Selenium? ( the way you do on Firefox)

Thank you.

Reply

Joe Colantonio April 18, 2013 at 1:55 pm

rony » Thanks Roy – Selenium IDE is a Firefox only plugin. Currently there is not something similar that can be used in IE.

Reply

Dan April 21, 2013 at 12:26 am

Much appreciated :)

Reply

kenny April 25, 2013 at 6:01 pm

Great info, as usual!

I did something a little different, where I was creating it as a Test Project and going from there. I’m stuck on trying to write out my results to a notepad file though, but that’s another topic I suppose :-)

Reply

sobi May 8, 2013 at 12:32 pm

Good starter tutorial, for people like me. Is there more tutorials on web testing using C#?

Thanks

Reply

Joe Colantonio May 8, 2013 at 10:58 pm

sobi » Thanks! I will try to post more on web testing using C#. Does anyone else recommend any other resources?

Reply

Leave a Comment

Previous post:

Next post: