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.

If Selenium’s selectByValue does not select the option you expect, first confirm that the page has a native HTML <select>, then pass the target option’s exact value attribute—not its visible label. Check that the option exists and is enabled, wait for asynchronously loaded options, reacquire the element if the page replaced it, and assert the selected value afterward.

What selectByValue matches

A native select option can have a visible label and a separate value. For example, in <option value="foo">Bar</option>, the label shown to a person is Bar, but selection by value must use foo. Passing Bar to selectByValue will not match unless the option’s value is also Bar.

The method is part of Selenium’s select-list support and applies to HTML <select> elements containing <option> elements. Selenium’s guide explicitly says this helper does not work with dropdowns implemented using JavaScript overlays such as div or li elements. Those controls need to be operated through their actual clickable or keyboard-accessible UI. Selenium: Working with select list elements.

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

Diagnose the failure in order

  1. Identify the actual control. Inspect the rendered DOM. If the interactive control is a native <select>, use Selenium’s Select helper. If it is a custom widget, inspect how its options open and select, then interact with those elements using the appropriate locators and clicks or keyboard actions.
  2. Read the live option values. Compare the argument with each option’s current value attribute. Do not infer the value from the displayed label. If no option has the exact value, correct the argument or wait for the intended option to appear.
  3. Check enabled state. Inspect both the select and the target option. Selenium’s select-list guide says disabled options may not be selected. It also notes that, starting with Selenium 4.5, a Select wrapper cannot be created for a disabled <select>.
  4. Check when the option appears. If the page fills the list after a request or another interaction, wait until the intended option exists before calling the selection method.
  5. Refresh stale references. Navigation or a render update may replace the select element. If Selenium reports a stale element, locate the select again after the update instead of reusing the old reference.
  6. Verify the outcome. Read the selected option and assert its value. For a multi-select, inspect all selected options and confirm that the intended option is among them.

Java: select by the exact value

Java’s Select.selectByValue(String) selects options whose value matches its argument. If no option matches, the API documents a NoSuchElementException. See the Java Select API.

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

// Assumes driver is an initialized WebDriver and the page is open.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
By countrySelect = By.id("country");
String expectedValue = "us";

WebElement selectElement = wait.until(
    ExpectedConditions.elementToBeClickable(countrySelect)
);
wait.until(d -> d.findElements(
    By.cssSelector("#country option[value='" + expectedValue + "']")
).size() > 0);

// Re-find after waiting in case the page replaced the control.
selectElement = driver.findElement(countrySelect);
Select select = new Select(selectElement);
select.selectByValue(expectedValue);

String actualValue = select.getFirstSelectedOption().getAttribute("value");
if (!expectedValue.equals(actualValue)) {
    throw new AssertionError("Expected value " + expectedValue + " but got " + actualValue);
}

Replace country and us with the locator and value in your page. The option-presence wait is useful when options are added dynamically; if the select itself is replaced during loading, the code reacquires it before constructing Select.

Python: wait for the matching option and assert

Selenium Python 4.49.0 documents select_by_value(value) and raises NoSuchElementException when the requested value is absent. Its API also exposes first_selected_option and all_selected_options. Consult the Python API documentation and the published implementation.

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select, WebDriverWait

# Assumes driver is an initialized WebDriver and the page is open.
wait = WebDriverWait(driver, 10)
select_locator = (By.ID, "country")
expected_value = "us"

wait.until(EC.element_to_be_clickable(select_locator))
wait.until(lambda d: d.find_elements(
    By.CSS_SELECTOR, f"#country option[value='{expected_value}']"
))

# Locate again after the asynchronous update, if one occurred.
select = Select(driver.find_element(*select_locator))
select.select_by_value(expected_value)

actual_value = select.first_selected_option.get_attribute("value")
assert actual_value == expected_value, (
    f"Expected {expected_value!r}; got {actual_value!r}"
)

If the select allows multiple selections, use all_selected_options and compare the values of the selected options rather than relying only on the first selected option.

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

JavaScript: await selection before checking

The Selenium JavaScript API exposes an asynchronous selectByValue(value). Await it before checking selected state. The API and published implementation describe matching by option value and failure when no matching option is found: JavaScript Select API and published implementation.

const { By, until } = require('selenium-webdriver');
const { Select } = require('selenium-webdriver/lib/select');

// Assumes driver is an initialized WebDriver and the page is open.
const selectLocator = By.id('country');
const expectedValue = 'us';

await driver.wait(until.elementLocated(selectLocator), 10000);
await driver.wait(async () => {
  const options = await driver.findElements(
    By.css(`#country option[value="${expectedValue}"]`)
  );
  return options.length > 0;
}, 10000);

// Locate after the option wait so a replaced control is not reused.
const selectElement = await driver.findElement(selectLocator);
const select = new Select(selectElement);
await select.selectByValue(expectedValue);

const selected = await select.getFirstSelectedOption();
const actualValue = await selected.getAttribute('value');
if (actualValue !== expectedValue) {
  throw new Error(`Expected ${expectedValue}, got ${actualValue}`);
}

Use the import path supported by the Selenium JavaScript package version in your project; binding APIs can change. If your installed version exposes the helper through a different supported entry point, keep the same sequence: locate the native select, wait for the option, await selection, then verify.

When the dropdown is custom, not a select

A custom dropdown may look like a select but be built from buttons, list items, or other elements. Wrapping a non-select element in Selenium’s Select helper is the wrong approach. Inspect the DOM and the widget’s behavior, then use its visible controls. A typical interaction pattern is:

  1. Locate and click the widget’s trigger.
  2. Wait for the option list to become visible.
  3. Locate the option by a stable attribute or accessible name, then click it or use the widget’s supported keyboard interaction.
  4. Assert the widget’s resulting displayed value or selected state.

The exact locator and assertion depend on the widget’s markup; there is no universal selectByValue replacement for custom controls.

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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common errors and fixes

Symptom Likely cause What to do
No matching option / NoSuchElementException The requested string is not an option’s exact value, or the option has not loaded yet. Inspect the current option values. Correct the argument or wait for the exact option to appear. Java and Python document this exception for a missing match.
The method does not work on the element The control is a custom overlay rather than a native HTML select. Inspect the actual DOM and use the widget’s trigger and option elements instead of Select.
Cannot create a Select for the element The located element is not a native select, or it is disabled. Confirm the tag and enabled state. Selenium’s guide notes that disabled-select wrapping is disallowed as of Selenium 4.5.
Stale element reference A page update replaced the select after Selenium located it. Wait for the update, then locate the select again and perform the operation on the fresh reference.
Command completes but the page shows another choice The action was not verified, the page changed state afterward, or the wrong value was supplied. Read the selected option after the action and assert its value. For multi-selects, inspect all selected options.
Selection fails only intermittently The test is racing asynchronous rendering or dependent UI updates. Wait for the exact option or relevant page state rather than using a fixed delay alone. Selenium’s troubleshooting guidance identifies synchronization as a common source of errors and recommends explicit waits where appropriate.

For further guidance on synchronization and stale references, see Selenium Troubleshooting Assistance and Understanding Common Errors.

Or skip the browser setup

If the task is to capture a page rather than interact with its form controls, ScreenshotNeo can return a screenshot or PDF through one GET request. It accepts cookie or consent banners like a visitor and removes 60+ known consent platforms, newsletter popups, and chat widgets before capture; those steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and the response identifies the page verdict and billing status in headers. It also provides an MCP server for AI agents, including Claude, Cursor, and other MCP clients. This is a capture alternative, not a replacement for Selenium when a test must select a form option.

Example cURL request (replace the URL with the page to capture):

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 documentation for API parameters. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots. Sign up for ScreenshotNeo and start with 1,000 free screenshots a month, no card required.

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.

Version considerations

The disabled-select wrapper restriction is specifically dated to Selenium 4.5 in the official guide. The Python API reference cited here is version 4.49.0, and the select-list guide page notes a last-modified date of September 16, 2026. Verify the API for the language binding and version used by your project if method behavior or imports differ.

Frequently Asked Questions

Does selectByValue use the option’s visible text?

No. It matches the option’s value attribute; the label and value can be different.

Can I use Selenium’s Select helper on a div-based dropdown?

No. The helper is for native HTML select and option elements. Custom controls must be operated through their own UI elements.

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.

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