I downloaded the C# client drivers and the IDE. I managed to record some tests and successfully ran them from the IDE. But now I want to do that using C#. I added all relevant DLL files (Firefox) to the project, but I don't have the Selenium
class. Some Hello, World! would be nice.

- 30,738
- 21
- 105
- 131

- 225
- 1
- 2
- 3
-
1I assume you've looked at this? http://seleniumhq.org/docs/03_webdriver.html#the-5-minute-getting-started-guide – Etienne de Martel Jun 13 '11 at 17:44
-
Have you even tried googling? Granted, there is a lot - maybe most of it? - of Java-related Selenium stuff, but there are definitely also basic C#/VS/Selenium tutorials out there that answer your question. – Frank H. Apr 24 '15 at 13:57
-
I downloaded the C# drivers and the IDE -> What do you mean by IDE? It's for writing C# selenium code? such as Visual Studio. Or, Is it Selenium IDE? – Ripon Al Wasim Mar 28 '17 at 05:31
-
Guess you are going to use Visual Studio for writing Selenium C# code and manage the code and build. – Ripon Al Wasim Mar 28 '17 at 05:35
9 Answers
From the Selenium Documentation:
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
class GoogleSuggest
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
//Notice navigation is slightly different than the Java version
//This is because 'get' is a keyword in C#
driver.Navigate().GoToUrl("http://www.google.com/");
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Cheese");
System.Console.WriteLine("Page title is: " + driver.Title);
driver.Quit();
}
}

- 6,511
- 8
- 46
- 66
Install the NuGet packet manager
Download link: https://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c
Create a C# console application
Right-click on the project → Manage NuGet Packages. Search for "Selenium" and install package
Selenium.Support
.
You are done now, and you are ready to write your code :)
For code with Internet Explorer, download the Internet Explorer driver.
Link: http://selenium-release.storage.googleapis.com/index.html
- Open 2.45 as its the latest release
- Download IEDriverServer_x64_2.45.0.zip or IEDriverServer_Win32_2.45.0.zip
- Extract and simply paste the .exe file at any location, for example C:\
- Remember the path for further use.
Overall reference link: Selenium 2.0 WebDriver with Visual Studio, C#, & IE – Getting Started
My sample code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.IE;
namespace Selenium_HelloWorld
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new InternetExplorerDriver("C:\\");
driver.Navigate().GoToUrl("http://108.178.174.137");
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("inputName")).SendKeys("apatra");
driver.FindElement(By.Id("inputPassword")).SendKeys("asd");
driver.FindElement(By.Name("DoLogin")).Click();
string output = driver.FindElement( By.XPath(".//*[@id='tab-general']/div/div[2]/div[1]/div[2]/div/strong")).Text;
if (output != null )
{
Console.WriteLine("Test Passed :) ");
}
else
{
Console.WriteLine("Test Failed");
}
}
}
}

- 30,738
- 21
- 105
- 131

- 271
- 2
- 7
- 18
To set up the IDE for Selenium in conjunction with C# is to use Visual Studio Express. And you can use NUnit as the testing framework. The below links provide you more details. It seems you have set up what is explained in the first link. So check the second link for more details on how to create a basic script.
How to setup C#, NUnit and Selenium client drivers on Visual Studio Express for Automated tests
Creating a basic Selenium web driver test case using NUnit and C#
Sample code from the above blog post:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
// Step a
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Firefox;
using NUnit.Framework;
namespace NUnitSelenium
{
[TestFixture]
public class UnitTest1
{
[SetUp]
public void SetupTest()
{
}
[Test]
public void Test_OpeningHomePage()
{
// Step b - Initiating webdriver
IWebDriver driver = new FirefoxDriver();
// Step c: Making driver to navigate
driver.Navigate().GoToUrl("http://docs.seleniumhq.org/");
// Step d
IWebElement myLink = driver.FindElement(By.LinkText("Download"));
myLink.Click();
// Step e
driver.Quit();
)
}
}

- 30,738
- 21
- 105
- 131

- 2,612
- 1
- 21
- 16
One of the things that I had a hard time finding was how to use PageFactory in C#. Especially for multiple IWebElements. If you wish to use PageFactory, here are a few examples. Source: PageFactory.cs
To declare an HTML WebElement, use this inside the class file.
private const string _ID ="CommonIdinHTML";
[FindsBy(How = How.Id, Using = _ID)]
private IList<IWebElement> _MultipleResultsByID;
private const string _ID2 ="IdOfElement";
[FindsBy(How = How.Id, Using = _ID2)]
private IWebElement _ResultById;
Don't forget to instantiate the page object elements inside the constructor.
public MyClass(){
PageFactory.InitElements(driver, this);
}
Now you can access that element in any of your files or methods. Also, we can take relative paths from those elements if we ever wish to. I prefer pagefactory because:
- I don't ever need to call the driver directly using driver.FindElement(By.Id("id"))
- The objects are lazy initialized
I use this to write my own wait-for-elements methods, WebElements wrappers to access only what I need to expose to the test scripts, and helps keeps things clean.
This makes life a lot easier if you have dynamic (autogerated) webelements like lists of data. You simply create a wrapper that will take the IWebElements and add methods to find the element you are looking for.

- 30,738
- 21
- 105
- 131

- 21
- 4
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"D:\DownloadeSampleCode\WordpressAutomation\WordpressAutomation\Selenium", "geckodriver.exe");
service.Port = 64444;
service.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
Instance = new FirefoxDriver(service);

- 21,252
- 9
- 60
- 109

- 31
- 2
-
1Please explain your answer. Code-only answers are discouraged here, as they don't teach anything - they just encourage copy-paste coding. You can use the edit link below your answer to add additional information. Thanks! – Tim Malone Sep 19 '16 at 23:05
C#
First of all, download Selenium IDE for Firefox from the Selenium IDE.
Use and play around with it, test a scenario, record the steps, and then export it as a C# or Java project as per your requirement.
The code file contains code something like:
using System; using System.IO; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; // Add this name space to access WebDriverWait using OpenQA.Selenium.Support.UI; namespace MyTest { [TestClass] public class MyTest { public static IWebDriver Driver = null; // Use TestInitialize to run code before running each test [TestInitialize()] public void MyTestInitialize() { try { string path = Path.GetFullPath(""); // Copy the Chrome driver to the debug // folder in the bin or set path accordingly Driver = new ChromeDriver(path); } catch (Exception ex) { string error = ex.Message; } } // Use TestCleanup to run code after each test has run [TestCleanup()] public void MyCleanup() { Driver.Quit(); } [TestMethod] public void MyTestMethod() { try { string url = "http://www.google.com"; Driver.Navigate().GoToUrl(url); IWait<IWebDriver> wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30.00)); // Wait in Selenium wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.XPath(@"//*[@id='lst - ib']"))); var txtBox = Driver.FindElement(By.XPath(@"//*[@id='lst - ib']")); txtBox.SendKeys("Google Office"); var btnSearch = Driver.FindElement(By.XPath("//*[@id='tsf']/div[2]/div[3]/center/input[1]")); btnSearch.Click(); System.Threading.Thread.Sleep(5000); } catch (Exception ex) { string error = ex.Message; } } } }
You need to get the Chrome driver from here.
You need to get NuGet packages and necessary DLL files for the Selenium NuGet website.
You need to understand the basics of Selenium from the Selenium documentation website.
That's all...

- 30,738
- 21
- 105
- 131

- 1,176
- 2
- 12
- 29
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using SeleniumAutomationFramework.CommonMethods;
using System.Text;
[TestClass]
public class SampleInCSharp
{
public static IWebDriver driver = Browser.CreateWebDriver(BrowserType.chrome);
[TestMethod]
public void SampleMethodCSharp()
{
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
driver.Url = "http://www.store.demoqa.com";
driver.Manage().Window.Maximize();
driver.FindElement(By.XPath(".//*[@id='account']/a")).Click();
driver.FindElement(By.Id("log")).SendKeys("kalyan");
driver.FindElement(By.Id("pwd")).SendKeys("kalyan");
driver.FindElement(By.Id("login")).Click();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>(d => d.FindElement(By.LinkText("Log out")));
Actions builder = new Actions(driver);
builder.MoveToElement(driver.FindElement(By.XPath(".//*[@id='menu-item-33']/a"))).Build().Perform();
driver.FindElement(By.XPath(".//*[@id='menu-item-37']/a")).Click();
driver.FindElement(By.ClassName("wpsc_buy_button")).Click();
driver.FindElement(By.XPath(".//*[@id='fancy_notification_content']/a[1]")).Click();
driver.FindElement(By.Name("quantity")).Clear();
driver.FindElement(By.Name("quantity")).SendKeys("10");
driver.FindElement(By.XPath("//*[@id='checkout_page_container']/div[1]/a/span")).Click();
driver.FindElement(By.ClassName("account_icon")).Click();
driver.FindElement(By.LinkText("Log out")).Click();
driver.Close();
}
}

- 30,738
- 21
- 105
- 131

- 11
- 1
- You will need to install Microsoft Visual Studio community Edition
- Create a new project as Test Project of C#
- Add Selenium references from the NuGet Package Manager. Then you will be all set.
- Create a new class and use [Test Class] and [Test Method] annotations to run your script
- You can refer to Run Selenium C# | Setup Selenium and C# | Configure Selenium C# for more details.

- 30,738
- 21
- 105
- 131

- 11
- 2
Use the below code once you've added all the required C# libraries to the project in the references.
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace SeleniumWithCsharp
{
class Test
{
public IWebDriver driver;
public void openGoogle()
{
// creating Browser Instance
driver = new FirefoxDriver();
//Maximizing the Browser
driver.Manage().Window.Maximize();
// Opening the URL
driver.Navigate().GoToUrl("http://google.com");
driver.FindElement(By.Id("lst-ib")).SendKeys("Hello World");
driver.FindElement(By.Name("btnG")).Click();
}
static void Main()
{
Test test = new Test();
test.openGoogle();
}
}
}

- 954
- 10
- 18

- 1
- 1