SeleniumHQ/selenium · error · Error
Cannot locate option with value: ${value}
Error message
Cannot locate option with value: ${value} What it means
Thrown by selectByValue() when no <option> whose @value attribute equals the argument was found (the `matched` flag stayed false after iterating candidates). It is a generic Error. Note selectByValue uses an XPath './/option[@value = "..."]', so the match is exact attribute equality.
Source
Thrown at javascript/selenium-webdriver/lib/select.js:239
* @param {string} value value of option element to be selected
*/
async selectByValue(value) {
let matched = false
let isMulti = await this.isMultiple()
let options = await this.element.findElements(By.xpath('.//option[@value = ' + escapeQuotes(value) + ']'))
for (let option of options) {
await this.setSelected(option)
if (!isMulti) {
return
}
matched = true
}
if (!matched) {
throw new Error(`Cannot locate option with value: ${value}`)
}
}
/**
*
* Select option with displayed text matching the argument.
*
* <example>
<select id="selectbox">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
const selectBox = await driver.findElement(By.id("selectbox"));
await selectObject.selectByVisibleText("Option 2");
* </example>
*
* @param {String|Number} text text of option element to get selectedView on GitHub (pinned to aa36b38e69)
Solutions
- Confirm the real @value: await option.getAttribute('value') for each option, or inspect the DOM.
- If the selectable identifier is text, use selectByVisibleText instead.
- Normalize case/whitespace before calling: selectByValue(value.trim()).
- If the value is partial, fall back to finding the option by XPath text match then clicking it.
Example fix
// before
await sel.selectByValue('New York'); // options have value="NY", text="New York"
// after
const val = await (await sel.getOptions()).find(async ...) ;
// simpler: select by visible text
await sel.selectByVisibleText('New York'); Defensive patterns
Strategy: validation
Validate before calling
async function findOptionByValue(sel, value) {
for (const o of await sel.getOptions()) {
if ((await o.getAttribute('value')) === value) return o;
}
return null;
}
// if (!await findOptionByValue(sel, v)) throw new Error('no such value'); Type guard
async function hasOptionWithValue(sel, value) {
for (const o of await sel.getOptions()) {
if ((await o.getAttribute('value')) === value) return true;
}
return false;
} Try / catch
try {
await sel.selectByValue(v);
} catch (e) {
if (/Cannot locate option with value/.test(e.message)) {
// fall back to visible text
await sel.selectByVisibleText(v);
} else throw e;
} Prevention
- Inspect the actual @value attribute, not the visible text.
- Normalize case and whitespace before matching.
- Distinguish 'value attr' from 'option text' in your data model.
When it happens
Trigger: selectByValue('NY') when options use lowercase 'ny'; value contains leading/trailing whitespace in the attribute but not in the arg; the value lives in option text, not the @value attribute; option has no value attribute (value defaults to text in HTML, but the XPath targets the attribute).
Common situations: Confusing option text with option value; case mismatch; dynamically generated values with prefixes/suffixes; trimmed vs. raw value in fixtures.
Related errors
- Cannot locate option with text: ${text}
- Invalid locator
- input must be a string
- Element must not be null. Please provide a valid <select> el
- Select only works on <select> elements
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/6f34c2b6b1093799.
Report an issue: GitHub.