Roadmap › Test Automation — Selenium, Playwright, API and BDD with C# › Module: UI Automation
Lessons in this course (10)
3. Selenium WebDriver with C# — locators and actions
Intermediate★ Must knowLesson 3 of 10
csharp
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
var options = new ChromeOptions();
options.AddArgument("--headless=new");
using IWebDriver driver = new ChromeDriver(options); // Selenium Manager downloads the driver automatically (4.6+)
driver.Navigate().GoToUrl("https://shop.example.com/login");
driver.FindElement(By.Id("email")).SendKeys("qa@example.com");
driver.FindElement(By.CssSelector("input[name='password']")).SendKeys("Secret#123");
driver.FindElement(By.XPath("//button[normalize-space()='Sign in']")).Click();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var heading = wait.Until(d => d.FindElement(By.CssSelector("[data-testid='dashboard-title']")));
Assert.That(heading.Text, Is.EqualTo("Dashboard"));Locator priority (most to least stable): dedicated test ids (data-testid) → id → name → CSS selectors → XPath → link text. Avoid absolute XPath (/html/body/div[3]/...) and auto-generated classes.
| XPath tricks | Example |
|---|---|
| Text | //button[text()='Save'], contains(text(),'Save') |
| Attribute contains | //input[contains(@id,'email')] |
| Axes | //label[.='Email']/following-sibling::input, //td[.='1001']/parent::tr//button |
Other APIs: Actions (hover, drag-drop, keyboard), SelectElement (dropdowns), SwitchTo().Frame()/Window()/Alert(), IJavaScriptExecutor (scroll, read values), GetScreenshot().