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

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

//Perform Ops

//Close the browser
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

24 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.


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

  4. Hi karthik,
    Very good session.can u explain how to replace the chromedriver.exe in project bin folder

    1. What do you mean replace ?

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

      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.

  5. 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

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

  7. 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.

  8. 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:

  9. 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

  10. hi Karthik,

    I would like to purchase the selenium c# advanced course. could you please share the coupon code

  11. Hi Karthik
    i read your tutorial and create a selenium automation in,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.

  12. Sir great But My question is How to run My html code with specific url with selenium web driver., now my i am using cefsharp web browser and cefsharpObject.HtmlLoad(“My Html code”,”URL That i want”,) this complete my task but getting time that is very bad my work,

Leave a Reply

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