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

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

For a native HTML <select>, find the element, wrap it in Selenium’s SelectElement, and call SelectByText. The call matches the displayed option text exactly by default; pass partialMatch: true only when a partial label is intentional.

Use SelectElement.SelectByText for a native dropdown

Selenium’s .NET support API provides SelectElement specifically for manipulating options in an HTML <select>. The constructor accepts the element returned by your locator, and SelectByText(string text, bool partialMatch = false) selects the option whose displayed label matches the supplied string.

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

IWebElement dropdown = driver.FindElement(By.Id("country"));
var select = new SelectElement(dropdown);
select.SelectByText("Canada");

This example assumes driver is an existing IWebDriver session and that the page contains a native control such as <select id="country">. The text is the human-visible label between the option tags, not necessarily the option’s value attribute.

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

What Selenium matches

Exact text is the default

SelectByText("Canada") requests an exact text match. If the option is displayed as “Canada,” that is the string your test should provide. A different label, unexpected whitespace, or a locator that points to another control can result in no match rather than an arbitrary selection.

The API documents partialMatch as optional and defaulting to false. Keep the default for deterministic tests whenever the complete label is known.

Use partial matching deliberately

When a page contains a predictable prefix or the full label is not known, use the overload explicitly:

var dropdown = driver.FindElement(By.Id("country"));
var select = new SelectElement(dropdown);
select.SelectByText("Can", partialMatch: true);

Partial matching broadens the set of labels that can satisfy the request. If more than one option can contain the fragment, the test can become ambiguous, so prefer an exact label or a different selection method when the requirement identifies a unique value.

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

Complete C# example

The following flow navigates to a page, locates a native dropdown, selects by its visible label, and reads back the selected option. Replace the URL and locator with those used by your application.

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

// driver is an already-created IWebDriver instance.
driver.Navigate().GoToUrl("https://example.test/profile");

IWebElement countryElement = driver.FindElement(By.Id("country"));
var country = new SelectElement(countryElement);
country.SelectByText("Canada");

IWebElement selected = country.SelectedOption;
Console.WriteLine(selected.Text);

SelectedOption exposes the first selected option. For a single-select list, that is the selected item you just chose. Reading it back is useful when the test needs to verify the control’s state rather than merely perform the action.

Choose the method that matches the requirement

Selenium documents three primary ways to select an option. Choose according to the information your test actually owns.

Method Use when the test knows Example Durability considerations
SelectByText The displayed, human-readable label SelectByText("Canada") Usually clearest for a requirement written in user-facing language. Exact matching is the default.
SelectByValue The option’s HTML value property SelectByValue("ca") Useful when the application contract specifies a stable value even if the label is translated.
SelectByIndex The option’s index attribute SelectByIndex(2) Order-dependent; avoid it when options can be inserted, removed, or reordered.

Both value and index selection have the same general contract: Selenium looks for the requested option and reports a missing option instead of silently selecting a different one. An index is therefore not a safer substitute for text; it is appropriate only when position is the actual business rule.

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

Prerequisites and the native-select boundary

The wrapped element must be a <select>

SelectElement is not a generic “dropdown” helper. Its constructor is documented for an HTML <select> element and can raise UnexpectedTagNameException when the element has another tag name. Before debugging the option text, inspect the markup your locator found.

IWebElement element = driver.FindElement(By.CssSelector("#country"));
Console.WriteLine(element.TagName);

If the output is not select, do not wrap it in SelectElement. Many modern controls are custom widgets composed of buttons, listboxes, and generated <div> elements. Their keyboard and click behavior must be automated according to that widget’s own DOM and accessibility contract; the native SelectElement API does not apply.

Find the intended control

A precise locator such as a stable id is preferable to a broad class selector when several lists appear on the page. If the page contains multiple controls with similar labels, verify that the locator returns the one associated with the field under test before calling SelectByText.

Handling exceptions and missing options

ArgumentNullException

The API documents ArgumentNullException when the text argument is null. Treat a null label as a test-data or application-state error instead of converting it to an empty string.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
string? label = testData.CountryLabel;
if (label is null)
{
    throw new ArgumentException("CountryLabel must contain an option label.", nameof(testData));
}

select.SelectByText(label);

NoSuchElementException

If no option has the requested text, Selenium documents NoSuchElementException. Common causes are an incorrect locator, a label that differs from the rendered text, selecting before the page has populated the list, or using exact matching where a partial match was intended.

Do not catch this exception and continue as if the selection succeeded. Instead, capture enough diagnostic context to identify the control and requested label, then fix the locator, data, or page-state synchronization.

UnexpectedTagNameException

This exception indicates that the element supplied to SelectElement is not a native select. Recheck the tag name and switch to the custom widget’s documented interaction model when necessary.

Multi-select lists and verifying state

A native select can allow multiple selections. The IsMultiple property tells you whether the control supports that mode. AllSelectedOptions exposes every selected item, while SelectedOption returns only the first selected item when several are selected.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
var select = new SelectElement(driver.FindElement(By.Id("skills")));

if (select.IsMultiple)
{
    select.SelectByText("C#");
    select.SelectByText("Selenium");

    foreach (IWebElement option in select.AllSelectedOptions)
    {
        Console.WriteLine(option.Text);
    }
}

Deselect operations are applicable only to a multi-select. Do not use a single SelectedOption assertion when the requirement is that several labels remain selected; inspect AllSelectedOptions instead.

Reliable selection patterns

Synchronize with application state

A locator can succeed before client-side code has finished adding options. If the list is populated after another field changes, perform the selection only after the expected option exists. A failure at that point is different from a bad label: the page may simply not have reached the state your test assumes.

Keep the synchronization condition tied to an observable page state, such as the presence of the expected option, rather than relying on a fixed delay. This makes the test less sensitive to machine speed while still allowing Selenium to report a genuine missing option.

Keep labels and test data explicit

Store the intended display label in test data when the requirement is written in user terms. If the application’s contract is a machine value, use SelectByValue and document that choice. Mixing a value such as ca into a text-selection call obscures the failure and makes maintenance harder.

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

Assert what matters

After selecting, read SelectedOption.Text for a label-based assertion or inspect the option’s value when the test is about the submitted form value. For multi-select controls, compare the set represented by AllSelectedOptions with the expected set; the first selected item alone is insufficient.

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

Troubleshooting checklist

  • “No option found”: Confirm the locator targets the intended native <select>, then compare the requested string with the option’s displayed text. Remember that exact matching is the default.
  • The constructor rejects the element: Inspect TagName. A custom button/listbox widget cannot be passed to SelectElement.
  • The option appears in the browser but the test cannot find it: Check whether the list is populated asynchronously and whether the test reaches the control before that update completes.
  • Partial matching selects the wrong item: Replace the fragment with a unique full label or select by a stable value.
  • The test passes but submits the wrong data: Verify whether the requirement concerns visible text or the option’s value; use the corresponding API and assertion.
  • Only one item is reported in a multi-select: Use AllSelectedOptions; SelectedOption intentionally returns the first selected item.
  • A null-label failure appears: Validate test data before calling SelectByText; null is documented as an argument error, not a request for an empty label.

WebDriver context and version notes

Selenium describes WebDriver as driving a browser natively, either locally or remotely, and identifies the protocol as a W3C Recommendation. The cited overview was last modified September 16, 2026. The API material does not establish a specific Selenium package, browser, or driver version for this example, so treat the code as the API-shaped pattern and verify compatibility in the versions used by your project.

Or skip the browser setup

If your goal is a visual capture rather than an interactive form assertion, ScreenshotNeo can return a screenshot or PDF from one request. It accepts the URL, handles the browser session for you, and has an MCP server for AI clients such as Claude and Cursor. Cookie-consent banners, newsletter popups, and chat widgets are removed before capture; bot checks, blank pages, failed loads, timeouts, and cache hits are not billed as clean shots.

See the ScreenshotNeo API documentation for all parameters. A direct cURL call is:

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.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

The equivalent C#-adjacent scripting examples are useful when a test pipeline already uses another language:

import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

The service reports page and billing outcomes in X-Page-Verdict and X-Billed headers. Every plan includes its features: full-page and element captures, device and viewport controls, retina scale, PDF output, custom CSS and JavaScript, clicks, waits, request blocking, headers, cookies, user agents, authorization, timezone and geolocation, transparent backgrounds, resizing, chosen cache TTLs, signed links, asynchronous webhooks, bulk capture of up to 100 URLs per call, a usage API, and an OpenAPI specification. Parameter names used by other screenshot APIs also work, which can simplify migration.

The Free plan includes 1,000 shots each month without a card. Paid plans start at $5 for 3,000 shots; yearly billing provides two months free. Create a free ScreenshotNeo account to try it.

Frequently Asked Questions

Can SelectByText select an option that is hidden by CSS?

It operates on the options exposed by the native select element. If the control has not been populated or the option is not part of that select, Selenium reports the documented missing-option error; inspect the rendered control and page state first.

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

Should a localization test select by label or value?

Select by the property your test is intended to validate. A label-based test can use the localized displayed text, while a submission-contract test can use the stable HTML value with SelectByValue.

Is SelectElement suitable for an ARIA listbox built from div elements?

No. Its constructor contract is for a native HTML select. An ARIA or JavaScript widget needs interactions that match its own DOM and accessibility behavior.

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.