{"record":{"id":"7f6aa7675cb15b59","repo":"SeleniumHQ/selenium","slug":"cannot-locate-option-with-text-text","errorCode":null,"errorMessage":"Cannot locate option with text: ${text}","messagePattern":"Cannot locate option with text: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"javascript/selenium-webdriver/lib/select.js","lineNumber":301,"sourceCode":"        candidates = await this.element.findElements(By.xpath(xpath))\n      }\n\n      const trimmed = text.trim()\n\n      for (let option of candidates) {\n        const optionText = await option.getText()\n        if (trimmed === optionText.trim()) {\n          await this.setSelected(option)\n          if (!(await this.isMultiple())) {\n            return\n          }\n          matched = true\n        }\n      }\n    }\n\n    if (!matched) {\n      throw new Error(`Cannot locate option with text: ${text}`)\n    }\n  }\n\n  /**\n   * Returns a list of all options belonging to this select tag\n   * @returns {!Promise<!Array<!WebElement>>}\n   */\n  async getOptions() {\n    return await this.element.findElements({ tagName: 'option' })\n  }\n\n  /**\n   * Returns a boolean value if the select tag is multiple\n   * @returns {Promise<boolean>}\n   */\n  async isMultiple() {\n    return this.multiple\n  }","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/javascript/selenium-webdriver/lib/select.js#L283-L319","documentation":"Thrown by selectByVisibleText() when no <option> whose trimmed text exactly equals the (trimmed) argument was found. Matching uses normalize-space(.) comparison, so leading/trailing whitespace is ignored but the full visible text must match exactly (case-sensitive, no partial).","triggerScenarios":"selectByVisibleText('United States') when the option text is 'United States of America' (partial doesn't match); case mismatch ('united states'); text differs by locale or non-breaking spaces; option text includes extra words.","commonSituations":"i18n/localized pages; options with parenthetical suffixes ('Option A (default)'); copy-pasted text with smart quotes; partial-match assumptions.","solutions":["Inspect actual text: const texts = await Promise.all((await sel.getOptions()).map(o => o.getText())); then pick.","Match exactly including case, or normalize: pick the option whose text startsWith the wanted prefix and click it.","If only a substring is known, find the option by XPath: By.xpath(`.//option[contains(normalize-space(.), '${frag}')]`) and click it.","Trim and compare unicode-normalized strings."],"exampleFix":"// before\nawait sel.selectByVisibleText('United'); // actual text is 'United States'\n\n// after\nconst opts = await sel.getOptions();\nfor (const o of opts) {\n  if ((await o.getText()).startsWith('United')) { await o.click(); break; }\n}","handlingStrategy":"validation","validationCode":"async function findOptionByText(sel, text) {\n  const t = String(text).trim();\n  for (const o of await sel.getOptions()) {\n    if ((await o.getText()).trim() === t) return o;\n  }\n  return null;\n}","typeGuard":"async function hasOptionWithText(sel, text) {\n  return Boolean(await findOptionByText(sel, text));\n}","tryCatchPattern":"try {\n  await sel.selectByVisibleText(t);\n} catch (e) {\n  if (/Cannot locate option with text/.test(e.message)) {\n    const opts = await sel.getOptions();\n    const match = opts.find(async o => (await o.getText()).includes(t));\n    if (match) await match.click(); else throw e;\n  } else throw e;\n}","preventionTips":["Match is exact + case-sensitive + trimmed; verify the real option text.","For partial matches use a contains() XPath manually.","Normalize unicode/case in fixtures for localized pages."],"tags":["select","text","locator","xpath","i18n"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}