{"record":{"id":"6f34c2b6b1093799","repo":"SeleniumHQ/selenium","slug":"cannot-locate-option-with-value-value","errorCode":null,"errorMessage":"Cannot locate option with value: ${value}","messagePattern":"Cannot locate option with value: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"javascript/selenium-webdriver/lib/select.js","lineNumber":239,"sourceCode":"   * @param {string} value value of option element to be selected\n   */\n  async selectByValue(value) {\n    let matched = false\n    let isMulti = await this.isMultiple()\n\n    let options = await this.element.findElements(By.xpath('.//option[@value = ' + escapeQuotes(value) + ']'))\n\n    for (let option of options) {\n      await this.setSelected(option)\n\n      if (!isMulti) {\n        return\n      }\n      matched = true\n    }\n\n    if (!matched) {\n      throw new Error(`Cannot locate option with value: ${value}`)\n    }\n  }\n\n  /**\n   *\n   * Select option with displayed text matching the argument.\n   *\n   * <example>\n   <select id=\"selectbox\">\n   <option value=\"1\">Option 1</option>\n   <option value=\"2\">Option 2</option>\n   <option value=\"3\">Option 3</option>\n   </select>\n   const selectBox = await driver.findElement(By.id(\"selectbox\"));\n   await selectObject.selectByVisibleText(\"Option 2\");\n   * </example>\n   *\n   * @param {String|Number} text       text of option element to get selected","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/javascript/selenium-webdriver/lib/select.js#L221-L257","documentation":"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.","triggerScenarios":"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).","commonSituations":"Confusing option text with option value; case mismatch; dynamically generated values with prefixes/suffixes; trimmed vs. raw value in fixtures.","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."],"exampleFix":"// before\nawait sel.selectByValue('New York'); // options have value=\"NY\", text=\"New York\"\n\n// after\nconst val = await (await sel.getOptions()).find(async ...) ;\n// simpler: select by visible text\nawait sel.selectByVisibleText('New York');","handlingStrategy":"validation","validationCode":"async function findOptionByValue(sel, value) {\n  for (const o of await sel.getOptions()) {\n    if ((await o.getAttribute('value')) === value) return o;\n  }\n  return null;\n}\n// if (!await findOptionByValue(sel, v)) throw new Error('no such value');","typeGuard":"async function hasOptionWithValue(sel, value) {\n  for (const o of await sel.getOptions()) {\n    if ((await o.getAttribute('value')) === value) return true;\n  }\n  return false;\n}","tryCatchPattern":"try {\n  await sel.selectByValue(v);\n} catch (e) {\n  if (/Cannot locate option with value/.test(e.message)) {\n    // fall back to visible text\n    await sel.selectByVisibleText(v);\n  } else throw e;\n}","preventionTips":["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."],"tags":["select","value","locator","xpath"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}