How to use Selenium with C# in Visual Studio Code?

fleek IT Solutions's picture

 How to use Selenium with C# in Visual Studio Code?

4 Answers

James Walker's picture

Hi,

Follow these instructions to utilize Selenium with C# in Visual Studio Code:

 

1. Download and install Visual Studio Code and the.NET Core SDK.

2. In Visual Studio Code, add the C# extension.

3. Open Visual Studio Code and create a new folder for your Selenium project.

4. In Visual Studio Code, open the integrated terminal.

5. Launch a new C# project by typing 'dotnet new console' into the terminal.

6. In the terminal, type 'dotnet add the package Selenium.WebDriver' to install the Selenium WebDriver package.

7. In the 'Program.cs' file, write your Selenium tests.

8. Save the file 'Program.cs'.

9. Run the tests by typing 'dotnet run' into the terminal.

 

That's all! In Visual Studio Code, you can now write and execute Selenium tests in C#.

 

Have a nice day.

 

 

 

 

ligem jim's picture
ligem jim replied on September 7, 2023 - 9:12am.

Step 1: Create a New Project

Open a terminal window in Visual Studio Code.

Navigate to the folder where you want to create your new project.

Type dotnet new console -n MySeleniumProject to create a new console application.

Step 2: Add Selenium WebDriver NuGet Package

Navigate into your project folder in the terminal (cd MySeleniumProject).

Type dotnet add package Selenium.WebDriver --version 3.141.0 to add the Selenium WebDriver package to your project.

Step 3: Add WebDriver Manager (Optional)

For ease of management, you can also add WebDriver Manager which will automatically manage the browser driver for you.

 

bash

Copy code

dotnet add package WebDriverManager --version 2.11.0

Step 4: Add Selenium Code

Open your Program.cs file and add the necessary using statements and Selenium code. Below is a sample code to open Google and search for "Hello World":

 

csharp

Copy code

using System;

using OpenQA.Selenium;

using OpenQA.Selenium.Chrome;

using WebDriverManager;

using WebDriverManager.DriverConfigs.Impl;

 

namespace MySeleniumProject

{

    class Program

    {

        static void Main(string[] args)

        {

            // Setup ChromeDriver using WebDriverManager

            new DriverManager().SetUpDriver(new ChromeConfig());

 

            // Initialize Chrome Driver

            IWebDriver driver = new ChromeDriver();

 

            // Navigate to Google

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

 

            // Find search box using its name attribute

            IWebElement searchBox = driver.FindElement(By.Name("q"));

 

            // Type "Hello World" in the search box

            searchBox.SendKeys("Hello World");

 

            // Submit the search

            searchBox.Submit();

 

            // Wait for a bit (this is not the best practice, but serves for demonstration purposes)

            System.Threading.Thread.Sleep(5000);

 

            // Close the Chrome browser

            driver.Quit();

        }

    }

}

Step 5: Run Your Code

Go back to the terminal and navigate to your project directory. Run the command dotnet run to execute your program. This should open up a new Chrome window and perform a Google search for "Hello World".

 

And there you have it! You've set up a Selenium WebDriver project in C# using Visual Studio Code.

Amir Shaikh's picture

Setting Up Selenium with C# in Visual Studio Code:

1. Create a New C# Project: Open a terminal, create a new folder for your project, and navigate into it. Run the following commands:

   dotnet new console -n YourProjectName

   cd YourProjectName

2. Install Selenium WebDriver: Install the Selenium.WebDriver NuGet package using the following command:

   dotnet add package Selenium.WebDriver

3. Install Selenium WebDriver Chrome Driver: Install the Selenium.WebDriver.ChromeDriver NuGet package for Chrome. Replace <version> with your preferred version:

   dotnet add package Selenium.WebDriver.ChromeDriver --version <version>

4. Open Project in Visual Studio Code: Launch Visual Studio Code and open your project folder:  

   code .

5. Write Selenium Code: Create a new C# file (e.g., Program.cs) and write your Selenium C# code.

Here's a simple example:

 

   using OpenQA.Selenium;

   using OpenQA.Selenium.Chrome;

   using System;

 

   class Program

   {

       static void Main()

       {

           var chromeDriverPath = "path/to/chromedriver";

 

           using (var driver = new ChromeDriver(chromeDriverPath))

           {

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

 

               IWebElement element = driver.FindElement(By.Id("elementId"));

               element.SendKeys("Hello, Selenium!");

 

               System.Threading.Thread.Sleep(5000);

           }

       }

   }

8. Run Your Selenium Code: In the terminal, type:

   dotnet run

This will compile and run your Selenium C# code.

Remember to replace `"path/to/chromedriver"` with the actual path to your ChromeDriver executable and ensure that ChromeDriver matches your Chrome browser version.

StickyMinds is a TechWell community.

Through conferences, training, consulting, and online resources, TechWell helps you develop and deliver great software every day.