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.