DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk5 min

Selenium WebDriver Browser Commands in C#: A Guide

A practical C# reference to Selenium WebDriver commands for navigation, page inspection, element interaction, waits, context switching and cleanup.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Selenium’s C# browser commands let you open pages, inspect the current browsing context, find and operate on elements, manage windows and cookies, and wait for application state. The examples below use the IWebDriver interface with ChromeDriver; other drivers can be used where supported. The official API is a living reference, so check signatures and capabilities against the Selenium package and browser driver installed in your project.

Start a browser session and navigate

IWebDriver is Selenium’s main interface for controlling a browser session. Create a concrete driver such as ChromeDriver, then use Navigate().GoToUrl(...) to load a page:

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

IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/web-form.html");

The Navigate() method returns an INavigation interface. Use it for direct URL navigation and browser-history actions. Setting driver.Url also loads a URL using HTTP GET; redirects can affect when control returns. See Selenium’s IWebDriver API and C# first-script example.

Navigation command choices

Need Command What it does
Load a known URL driver.Navigate().GoToUrl(url) or set driver.Url Requests the URL and waits according to the configured page-load strategy.
Return to the prior history entry driver.Navigate().Back() Uses the browser’s back navigation.
Move forward in history driver.Navigate().Forward() Uses the browser’s forward navigation.
Reload the current page driver.Navigate().Refresh() Refreshes the current browsing context.

Inspect the current browsing context

These IWebDriver properties describe the selected browser window and its current page:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • driver.Url gets the current URL.
  • driver.Title gets the current window’s title.
  • driver.PageSource returns a representation of the page’s underlying DOM.
  • driver.CurrentWindowHandle identifies the selected window, while driver.WindowHandles lists the open window handles.

Do not treat PageSource as a guaranteed snapshot of the live DOM after JavaScript changes the page. Selenium’s API documentation notes that it may not reflect post-load modifications and is not necessarily formatted like the original server response.

Find elements and interact with them

Use By to specify how to locate an element, and use the returned IWebElement to interact with it. FindElement is for a match you expect to exist; FindElements returns a collection and is useful when there may be zero or multiple matches.

var textBox = driver.FindElement(By.Name("my-text"));
var submitButton = driver.FindElement(By.TagName("button"));
textBox.SendKeys("Selenium");
submitButton.Click();
var message = driver.FindElement(By.Id("message")).Text;

Common locator strategies include By.Id(...), By.Name(...), By.TagName(...) and other By methods. Choose a locator that identifies the intended element reliably on the page. The IWebElement API provides actions such as Click() and SendKeys(...), and properties such as Text. The official C# example demonstrates the form interaction above.

Element references can become stale when the page replaces or redraws the corresponding DOM node. If that happens, wait for the new state and locate the element again rather than assuming an earlier reference remains valid. Use Selenium’s element finders guidance for locator details.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Wait for the state your next command needs

A navigation command waits for a document readyState determined by the page-load strategy; the default target is complete. That does not guarantee that a JavaScript application has finished rendering the control your test needs. Waiting for the specific condition before acting helps avoid timing races.

Condition-specific explicit wait

A WebDriverWait can repeatedly evaluate a condition until it succeeds or its timeout expires. This example waits up to three seconds for an element to be found:

using OpenQA.Selenium.Support.UI;

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
var element = wait.Until(d => d.FindElement(By.Name("q")));

Use a condition that corresponds to the action you need next, such as an element becoming available. The .NET WebDriverWait API documents the wait interface.

Implicit wait versus explicit wait

An implicit wait is a session-wide setting that affects element searches; an explicit wait is applied at a particular point and tests a stated condition. Prefer explicit waits when the application’s required state is specific. Selenium warns: “Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times.” See the official Waiting Strategies guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Manage timeouts, cookies, windows, and frames

driver.Manage() provides access to browser-session options, including timeouts, cookies and window controls. The precise methods available depend on the installed .NET API; consult its namespace reference for the version in your project.

driver.SwitchTo() selects a target context, such as a frame or another window. Subsequent driver commands operate in that selected context. Switch back when the next action belongs to the parent page or another window. For windows, use WindowHandles to inspect open handles and select the intended one through the target locator.

Close a window or end the session

driver.Close() closes the current window; if it is the last window, the browser quits. driver.Quit() ends the WebDriver session and closes all windows associated with it. Use Quit() in cleanup so the session is ended even if an assertion or command fails:

IWebDriver driver = new ChromeDriver();
try
{
    driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/web-form.html");
    // Run test steps and assertions here.
}
finally
{
    driver.Quit();
}

In a test framework, put session cleanup in the framework’s teardown or equivalent guaranteed cleanup path.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Other Selenium command families

The .NET WebDriver class also exposes JavaScript execution and screenshot-related functionality. Selenium’s namespace reference lists additional interfaces for alerts, logs, actions, printing, downloads and network handling. These are useful when a test requires those capabilities, but actual support and behavior can vary by browser driver; check the relevant API and driver documentation before relying on a command.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common command problems

Element search fails

If FindElement cannot locate a match, check that the locator targets the correct element and that the page has reached the needed state. For dynamic content, wait for the condition and then search again. Use FindElements when an empty result is an expected possibility.

An element reference is stale

A page update may have replaced the DOM node represented by the reference. Wait for the updated page state and locate the element again before interacting with it.

The page loaded, but a control is not ready

Page-load completion does not establish that client-side rendering or application work is finished. Wait explicitly for the element or state needed by the next test step.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Wait duration behaves unpredictably

Check whether the session combines implicit and explicit waits. Selenium advises against mixing them; use a consistent wait strategy, usually a condition-specific explicit wait for dynamic page behavior.

Commands target the wrong window or frame

Check the selected context. Inspect WindowHandles and use SwitchTo() to select the intended window or frame before issuing commands.

Or skip the browser setup

If your goal is to capture a page image or PDF rather than automate browser interactions, ScreenshotNeo offers a one-request screenshot API. It removes cookie and consent banners, newsletter popups and chat widgets before capture; bot checks, blank pages and failed loads are never billed. Its MCP server lets AI agents take screenshots, and 1,000 screenshots a month are free with no card; paid plans start at $5 for 3,000.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for options and response details. Sign up for 1,000 free screenshots a month, with no card required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.