Windows PowerShell  

Automating Chrome Browser with Selenium in C#

Selenium is one of the most widely used tools for web automation and testing. It allows developers and testers to automate web browsers to perform tasks such as testing, scraping, or repetitive browsing actions. When working with C#, Selenium WebDriver can be integrated seamlessly with Chrome to create robust automation scripts.

Why Use Selenium with C#?

  • Cross-browser automation: Selenium supports multiple browsers like Chrome, Firefox, Edge, and Safari.

  • Open-source: Selenium is free to use and has a large community.

  • Integration: Easily integrates with testing frameworks like NUnit, MSTest, and xUnit.

  • Supports multiple programming languages: Java, Python, Ruby, C#, etc.

Setting Up Selenium with Chrome in C#

Step 1: Install Visual Studio

Make sure you have Visual Studio installed. You can download it from the Visual Studio Official Site.

Step 2: Create a C# Console Project

  1. Open Visual Studio → Create a new project → Select Console App (.NET Framework) or .NET Core.

  2. Name your project and click Create.

Step 3: Install Selenium WebDriver

You can install Selenium WebDriver via NuGet Package Manager.

  1. Right-click your project → Manage NuGet Packages.

  2. Search for Selenium.WebDriver and Selenium.WebDriver.ChromeDriver.

  3. Install both packages.

Or, using the Package Manager Console:

Install-Package Selenium.WebDriver
Install-Package Selenium.WebDriver.ChromeDriver

Step 4: Write Selenium Chrome Automation Code

Here’s a basic example to open Chrome, navigate to Google, search for a query, and print the first result:

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

namespace SeleniumChromeExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Set ChromeDriver options (optional)
            ChromeOptions options = new ChromeOptions();
            options.AddArgument("--start-maximized"); // open browser in maximized mode
            options.AddArgument("--disable-infobars"); // disabling infobars

            // Initialize ChromeDriver
            using (IWebDriver driver = new ChromeDriver(options))
            {
                try
                {
                    // Navigate to Google
                    driver.Navigate().GoToUrl("https://www.google.com");

                    // Find search box using name attribute
                    IWebElement searchBox = driver.FindElement(By.Name("q"));
                    searchBox.SendKeys("Selenium C# tutorial");
                    searchBox.SendKeys(Keys.Enter);

                    // Wait for search results
                    System.Threading.Thread.Sleep(2000);

                    // Get first result
                    IWebElement firstResult = driver.FindElement(By.CssSelector("h3"));
                    Console.WriteLine("First Result: " + firstResult.Text);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
                finally
                {
                    // Close the browser
                    driver.Quit();
                }
            }
        }
    }
}

Step 5: Run Your Script

Press F5 to run your console application. Chrome will launch, perform the search, and print the first search result in the console.

Tips for Selenium Chrome Automation

  1. Use Explicit Waits: Avoid Thread.Sleep(); use WebDriverWait for dynamic waits.

  2. Headless Mode: Run Chrome in headless mode for background automation:

    options.AddArgument("--headless");
    
  3. Handle Pop-ups and Alerts: Use driver.SwitchTo().Alert() to handle browser alerts.

  4. Take Screenshots: Use ((ITakesScreenshot)driver).GetScreenshot().SaveAsFile("screenshot.png");

External Resources

Conclusion

Selenium WebDriver with Chrome in C# provides a powerful platform for browser automation. By following this setup, you can automate repetitive web tasks, perform UI testing, and integrate automation scripts into your CI/CD pipelines.