{"record":{"id":"cb57bd9387f4848d","repo":"SeleniumHQ/selenium","slug":"option-with-index-index-not-found-select-ele","errorCode":null,"errorMessage":"Option with index \"${index}\" not found. Select element only contains ${options.length - 1} option elements","messagePattern":"Option with index \"(.+?)\" not found\\. Select element only contains (.+?) option elements","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"javascript/selenium-webdriver/lib/select.js","lineNumber":194,"sourceCode":"   const selectBox = await driver.findElement(By.id(\"selectbox\"));\n   await selectObject.selectByIndex(1);\n   * </example>\n   *\n   * @param index\n   */\n  async selectByIndex(index) {\n    if (index < 0) {\n      throw new Error('Index needs to be 0 or any other positive number')\n    }\n\n    let options = await this.element.findElements(By.tagName('option'))\n\n    if (options.length === 0) {\n      throw new Error(\"Select element doesn't contain any option element\")\n    }\n\n    if (options.length - 1 < index) {\n      throw new Error(\n        `Option with index \"${index}\" not found. Select element only contains ${options.length - 1} option elements`,\n      )\n    }\n\n    for (let option of options) {\n      if ((await option.getAttribute('index')) === index.toString()) {\n        await this.setSelected(option)\n      }\n    }\n  }\n\n  /**\n   *\n   * Select option by specific value.\n   *\n   * <example>\n   <select id=\"selectbox\">\n   <option value=\"1\">Option 1</option>","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/javascript/selenium-webdriver/lib/select.js#L176-L212","documentation":"Thrown by selectByIndex() when the requested index exceeds the last valid option index (options.length - 1). NOTE the message is misleading: it says the select 'only contains N-1 option elements' where N is options.length — i.e. it reports one fewer option than actually present. Treat the bound as options.length-1 (the message's number) + 1 = real count.","triggerScenarios":"selectByIndex(5) on a select with 4 options (valid 0..3); hard-coded index from a fixture that drifted after the page changed; index derived from a length check that is off by one.","commonSituations":"Page redesign reduces the number of options; locale variants with fewer options; stale test data; assuming length == last index.","solutions":["Derive the index dynamically from the real option count: const n = (await sel.getOptions()).length; selectByIndex(Math.min(wanted, n - 1)).","Prefer selecting by visible text or value which is resilient to reordering/count changes.","Clamp and warn: if (index > n - 1) { index = n - 1; log.warn(...); }.","Fixtures: keep test option data in sync with the page or assert the expected count first."],"exampleFix":"// before\nawait sel.selectByIndex(5); // page now has only 4 options\n\n// after\nconst count = (await sel.getOptions()).length;\nawait sel.selectByIndex(Math.min(5, count - 1));","handlingStrategy":"validation","validationCode":"async function safeSelectByIndex(sel, idx) {\n  const n = (await sel.getOptions()).length;\n  if (n === 0) throw new Error('no options');\n  if (idx > n - 1) idx = n - 1;\n  await sel.selectByIndex(idx);\n}","typeGuard":"async function indexInRange(sel, idx) {\n  const n = (await sel.getOptions()).length;\n  return idx >= 0 && idx < n;\n}","tryCatchPattern":"try {\n  await sel.selectByIndex(idx);\n} catch (e) {\n  if (/Option with index/.test(e.message)) {\n    const n = (await sel.getOptions()).length;\n    await sel.selectByIndex(n - 1);\n  } else throw e;\n}","preventionTips":["Derive indices from the live option count.","Prefer value/text selection over positional indices.","Keep test fixtures in sync with the page or assert the count first."],"tags":["select","index","bounds","off-by-one"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}