SeleniumHQ/selenium · error · NoSuchElementException
Cannot locate option with value: {value}
Error message
Cannot locate option with value: {value} What it means
`select_by_value()` builds a CSS selector `option[value=...]` and tries to select matching options. If none match it raises `NoSuchElementException` so the caller knows the requested value does not exist in the select.
Source
Thrown at py/selenium/webdriver/support/select.py:85
`<option value="foo">Bar</option>`
Args:
value: The value to match against
Raises:
NoSuchElementException: If there is no option with specified value in SELECT
"""
css = f"option[value ={self._escape_string(value)}]"
opts = self._el.find_elements(By.CSS_SELECTOR, css)
matched = False
for opt in opts:
self._set_selected(opt)
if not self.is_multiple:
return
matched = True
if not matched:
raise NoSuchElementException(f"Cannot locate option with value: {value}")
def select_by_index(self, index: int) -> None:
"""Select the option at the given index by examining the "index" attribute.
Args:
index: The option at this index will be selected
Raises:
NoSuchElementException: If there is no option with specified index in SELECT
"""
match = str(index)
for opt in self.options:
if opt.get_attribute("index") == match:
self._set_selected(opt)
return
raise NoSuchElementException(f"Could not locate element with index {index}")
def select_by_visible_text(self, text: str) -> None:View on GitHub (pinned to aa36b38e69)
Solutions
- Inspect the select's option values first: `[o.get_attribute('value') for o in select.options]`
- Verify the value string matches exactly (case, whitespace)
- Add an explicit wait for the option to be present before selecting
- Catch NoSuchElementException and fall back to selecting by visible text or index
Example fix
// before
select.select_by_value("USA")
// after
values = [o.get_attribute("value") for o in select.options]
if "us" in values:
select.select_by_value("us") Defensive patterns
Strategy: try-catch
Validate before calling
values = [o.get_attribute("value") for o in select.options]
if value in values:
select.select_by_value(value) Type guard
def value_exists(select_obj, value) -> bool:
return value in [o.get_attribute("value") for o in select_obj.options] Try / catch
from selenium.common.exceptions import NoSuchElementException
try:
select.select_by_value(value)
except NoSuchElementException:
# value not present — handle/log
pass Prevention
- Snapshot option values before selecting from external data
- Match value strings exactly (case and whitespace)
- Add an explicit wait for the option when options load dynamically
When it happens
Trigger: Calling `select.select_by_value('foo')` when no `<option value="foo">` exists. Common when the value comes from test data that is stale, misspelled, or belongs to a different locale/environment.
Common situations: Value sourced from a config/env that differs across environments; trailing whitespace or case mismatch between data and the DOM attribute; the option is added dynamically and not yet present; hidden options that are present but the value string is wrong.
Related errors
- Could not locate element with index {index}
- Could not locate element with visible text: {text}
- Could not locate element with value: {value}
- Select only works on <select> elements, not on {webelement.t
- No options are selected
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/61a0cca60366ea9a.
Report an issue: GitHub.