Site Loader
Auckland, New Zealand
In this post we will discuss writing some simple code with Selenium C# in Visual Studio IDE. In the last post we referenced our Selenium WebDriver and Chrome driver via Nuget package manager in our project. Please read my previous post to get started with selenium referencing in Visual Studio IDE. Once you are done with setup (referencing), you are all set you write a simple code. Here is the code snippet
//Create the reference for our browser
IWebDriver driver = new ChromeDriver();

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

//Find the Search text box UI Element
IWebElement element = driver.FindElement(By.Name("q"));

//Perform Ops
element.SendKeys("executeautomation");

//Close the browser
driver.Close();
In the above code snippet, we have
  1. Created an instance for IWebDriver and got the instance for ChromeDriver
  2. Navigate to Google home page
  3. Found the Search textbox of google home page
  4. Searched for execute automation by typing the text
  5. Close the driver instance (which closes the chrome browser)
Here is the complete video for the above explanation Thanks for watching the video and reading the post !! Please leave your comments and let me know if there is anything need to be improved in the post !!! Thanks, Karthik KK

Post Author: Karthik kk

22 Replies to “Simple code with Selenium C#”

  1. Thanks karthik, really useful for beginners. Appreciate your hard work for giving step by step information for free of cost though there are many sites charging lot of fee.

  2. Hi Karthik,
    It really very nice and very useful for everyone. Could you please share some videos on Grid with C# so it will be complete.

    Thanks

  3. Gives ‘System.InvalidOperationException’ while creating the chrome driver instance (line no.3). However, works fine with FirefoxDriver.

    1. What do you mean replace ?

      Can you please update to latest chrome driver via Nuget if you want to.

      Thanks,
      Karthik KK

      1. I also encountered the same error because I selected the wrong chrome driver (it is hard to know which is correct because there are 7 listed). I fixed it by installing the chrome driver which has the highest number of downloads.

  4. Hi Karthik,

    when i try the same snippet from the current page i am getting an exception “An unhandled exception of type ‘System.InvalidOperationException’ occurred in WebDriver.dll” i am clue less why

  5. Gives ‘System.InvalidOperationException’ while creating the Internet explorer driver instance (line no.3). However, works fine with ChromeDriver.

  6. Hi Karthik,
    This is awesome article… i really liked it …. When i execute the code… i got excetion handling error…
    In the code above at line 2 (IWebDriver driver = new ChromeDriver();)
    Error -An unhandled exception of type ‘System.InvalidOperationException’ occurred in WebDriver.dll

    What action need to be taken?
    Your help is greatly appreciated.

  7. Hi Karthik,

    How to Launch Private Mode IE browser with Selenium C#, I tried two ways but its not working, can you please help here

    Method 1:

    InternetExplorerOptions options = new InternetExplorerOptions();
    options.BrowserCommandLineArguments = “-private”;
    options.IgnoreZoomLevel = true;
    driver = new InternetExplorerDriver(driverfolder,options);

    Method 2:
    driver.Navigate().GoToUrl(“about:InPrivate”);
    driver.Navigate().GoToUrl(url);

  8. deploy to chromedriver exe is not supported and the exe of file how to config in the deployment side and chrome driver and i make web api access to access the concept
    all way completed but my problem deploy to execute web api in c# that is my question

  9. Hi Karthik
    i read your tutorial and create a selenium automation in asp.net,c# website. That project working fine on local system but project is deployed to server that project is not working and no gatting any error. I also read many website but i do not get a solution. So please help my issue.

Leave a Reply

Your email address will not be published. Required fields are marked *