Getting Started with Slenium

Published on 27 April 2023 at 23:25

Selenium is an open source web application testing framework that is widely used for automating web browsers. It allows developers to write scripts in different programming languages such as Java, Python, C#, Ruby, etc., to automate web browsers like Chrome, Firefox, and Edge.

In this blog, we will focus on writing simple scripts with Selenium and C#.

Prerequisites:

  • Visual Studio IDE installed on your computer
  • Selenium.WebDriver and Selenium.Support NuGet packages added to your project
  • ChromeDriver executable downloaded and added to your project
  1. Create a new project
    Open Visual Studio and create a new project. Choose the "Console App (.NET Framework)" template.
  2. Add Selenium NuGet packages
    Right-click on your project in the Solution Explorer and select "Manage NuGet Packages." Search for Selenium.WebDriver and Selenium.Support, and install both packages.
  3. Download ChromeDriver
    Download the ChromeDriver executable for your Chrome browser version from the official website: https://sites.google.com/a/chromium.org/chromedriver/downloads. Once downloaded, extract the file and copy it to your project folder.
  4. Write the code
    Open Program.cs file and add the following code:
Getting Started With Selenium C# Sample Code
          
      using System;
      using OpenQA.Selenium;
      using OpenQA.Selenium.Chrome;
      
      namespace SeleniumCSharpDemo
      {
          class Program
          {
              static void Main(string[] args)
              {
                  // Create a new instance of the Chrome driver
                  IWebDriver driver = new ChromeDriver();
      
                  // Navigate to Google
                  driver.Navigate().GoToUrl("https://www.google.com");
      
                  // Find the search box element by its name
                  IWebElement searchBox = driver.FindElement(By.Name("q"));
      
                  // Type "Selenium C# Tutorial" into the search box
                  searchBox.SendKeys("Selenium C# Tutorial");
      
                  // Submit the search
                  searchBox.Submit();
      
                  // Wait for the results to load
                  driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
      
                  // Print the page title
                  Console.WriteLine("Page title: " + driver.Title);
      
                  // Close the browser
                  driver.Quit();
      
                  Console.ReadLine();
              }
          }
      }
    
  

This code will open the Chrome browser, navigate to Google, search for "Selenium C# Tutorial," wait for the results to load, print the page title, and then close the browser.

5. Run the script
Save the file and run the script by pressing F5. You should see the Chrome browser open, and the script will execute. Once the script is finished, the browser will close, and the console application will wait for you to press a key to exit.

Selenium is a powerful tool that allows developers to automate web browsers and perform various tests on web applications. In this blog, we have demonstrated how to write simple scripts with Selenium and C# to automate web browsers and perform basic actions like navigating to a website, searching for keywords, and printing page titles. With this knowledge, you can start exploring more advanced features of Selenium and build robust web testing automation frameworks.

Add comment

Comments

There are no comments yet.