Simple code with Selenium C#

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

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

18 comments

  1. shravan says:

    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. Deepthi says:

    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. Vikas says:

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

    • Karthik kk says:

      What do you mean replace ?

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

      Thanks,
      Karthik KK

      • aa says:

        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. siddu says:

    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. Divya says:

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

  6. Sarvani says:

    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. Anudeep says:

    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);

Leave a Reply

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