How to use Selenium with C#?

FREE Online Courses: Transform Your Career – Enroll for Free!

Selenium is a popular open-source automation tool for evaluating web applications. It allows developers to simulate user actions on a web page and automate browser testing. Selenium supports several programming languages, including C#. In this article, we will provide a comprehensive tutorial on how to use Selenium with C#.

Prerequisites to use Selenium with C#

Before getting started with Selenium C#, you need to have Visual Studio installed on your system. You can download and install the latest version of Visual Studio from the official website. Additionally, you need to install the Selenium C# bindings. You can do this using the NuGet package manager by executing the following command:

Install-Package Selenium.WebDriver

Once you have installed Visual Studio and the Selenium C# bindings, you are ready to get started.

Step 1: Launching the Browser

To launch a web browser using Selenium with C#, you need to create an instance of the browser driver. Selenium supports several browser drivers, including Chrome, Firefox,

and Edge. In this tutorial, we will be using the Chrome browser driver. You can download the Chrome driver from the official website.

Once you have downloaded the Chrome driver, you need to add it to your Visual Studio project. You can do this by right-clicking on the project in the Solution Explorer and selecting “Add” -> “Existing Item”. Then, select the Chrome driver file and click “Add”.

Next, you need to create an instance of the Chrome browser driver in your C# code. You can do this using the following code:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;


IWebDriver driver = new ChromeDriver("path/to/chromedriver");

This code creates an instance of the Chrome browser driver and opens a new Chrome window.

Step 2: Navigating to a Web Page

To navigate to a web page using Selenium with C#, you can use the Navigate().GoToUrl() method of the driver object. For instance, you can use the following code to go to the Google home page:

driver.Navigate().GoToUrl("https://www.google.com");

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

This code navigates to the Google homepage and loads the page content.

Step 3: Locating Elements

Selenium provides several methods for locating elements on a web page. You can locate elements using their ID, class name, name, link text, or XPath. Once you have located an element, you can interact with it using the provided methods, such as Click(), SendKeys(), and GetAttribute() .

For example, to locate the search box on the Google homepage and enter a search query, you can use the following code:

IWebElement searchBox = driver.FindElement(By.Name("q"));
searchBox.SendKeys("Selenium C# tutorial");
searchBox.Submit();

This code locates the search box element by its name attribute, enters a search query, and submits the form.

Step 4: Handling Alerts

Web pages sometimes display alert messages that require user interaction. Selenium provides a method for handling alert messages using the SwitchTo().Alert() method. You can use this method to accept or dismiss an alert message.

For example, to handle an alert message that requires the user to confirm their action, you can use the following code:

IAlert alert = driver.SwitchTo().Alert();
alert.Accept();

This code switches the driver’s focus to the alert message, accepts the message, and switches the focus back to the web page.

Step 5: Closing the Browser

Once you have completed your tests, you need to close the browser window using the Close() method of the driver object. For example:

driver.Close();

This code closes the browser window and terminates the Selenium session.

Example of using Selenium with C#

Here is an example that demonstrates the above steps in a complete Selenium C# script:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;


class Program
{
   static void Main(string[] args)
   {
       IWebDriver driver = new ChromeDriver("path/to/chromedriver");


       driver.Navigate().GoToUrl("https://www.google.com");


       IWebElement searchBox = driver.FindElement(By.Name("q"));
       searchBox.SendKeys("Selenium C# tutorial");
       searchBox.Submit();
       driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);


       Console.WriteLine(driver.Title);


       driver.Close();
   }
}

IMAGE

This code launches a Chrome browser window, navigates to the Google homepage, enters a search query, waits for the search results to load, and prints the title of the search results page. Finally, it closes the browser window.

Conclusion

In this tutorial, we have provided a comprehensive guide on how to use Selenium with C#. We have covered the basic steps involved in creating a Selenium script, including launching the browser, navigating to a web page, locating elements, handling alerts, and closing the browser. With the knowledge gained from this tutorial, you should be able to start writing your own Selenium scripts using C#. Happy testing!

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

Leave a Reply

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