{"record":{"id":"76c624d9efbf3db3","repo":"SeleniumHQ/selenium","slug":"select-element-doesn-t-contain-any-option-element","errorCode":null,"errorMessage":"Select element doesn't contain any option element","messagePattern":"Select element doesn't contain any option element","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"javascript/selenium-webdriver/lib/select.js","lineNumber":190,"sourceCode":"   <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.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.","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/javascript/selenium-webdriver/lib/select.js#L172-L208","documentation":"Thrown by selectByIndex() when the select element contains zero <option> children (findElements returned an empty array). A <select> with no options is structurally valid HTML, so this guards against an unselectable control. Generic Error.","triggerScenarios":"selectByIndex on a <select> whose options are populated asynchronously by the page (e.g. via fetch) and have not loaded yet; a genuinely empty <select></select>; an options list hidden behind a dependent dropdown that hasn't been triggered.","commonSituations":"Cascading selects where the second is empty until the first is chosen; SPA that renders options after data load; acting before the page is ready.","solutions":["Wait for at least one option: await driver.wait(driver.until.elementLocated(By.css('select#x option'))).","Trigger the parent dropdown / data fetch first, then poll for options.","Check option count before selecting: if ((await sel.getOptions()).length === 0) handleEmpty();","Add an explicit wait condition rather than a fixed sleep."],"exampleFix":"// before\nconst sel = new Select(await driver.findElement(By.id('city')));\nawait sel.selectByIndex(0); // empty -> throws\n\n// after\nawait driver.wait(async () => (await driver.findElements(By.css('select#city option'))).length > 0, 5000);\nconst sel = new Select(await driver.findElement(By.id('city')));\nawait sel.selectByIndex(0);","handlingStrategy":"retry","validationCode":"async function hasOptions(sel) {\n  return (await sel.getOptions()).length > 0;\n}\n// await driver.wait(hasOptions.bind(null, sel), 5000) before selectByIndex","typeGuard":"async function selectHasOptions(sel) {\n  return (await sel.getOptions()).length > 0;\n}","tryCatchPattern":"try {\n  await sel.selectByIndex(0);\n} catch (e) {\n  if (/doesn't contain any option/.test(e.message)) {\n    await driver.wait(async () => (await sel.getOptions()).length > 0, 5000);\n    await sel.selectByIndex(0);\n  } else throw e;\n}","preventionTips":["Wait for at least one option before interacting.","Trigger parent/data-fetch in dependent selects first.","Use driver.until conditions, not fixed sleeps."],"tags":["select","options","timing","async","wait"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}