{"record":{"id":"a9882428a65362cc","repo":"SeleniumHQ/selenium","slug":"index-needs-to-be-0-or-any-other-positive-number","errorCode":null,"errorMessage":"Index needs to be 0 or any other positive number","messagePattern":"Index needs to be 0 or any other positive number","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"javascript/selenium-webdriver/lib/select.js","lineNumber":184,"sourceCode":"  /**\n   *\n   * Select option with specified index.\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.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      }","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/javascript/selenium-webdriver/lib/select.js#L166-L202","documentation":"Thrown by selectByIndex() when the index argument is less than zero. It is a generic Error. Note there is no type check — a non-number (e.g. a string) will be coerced by the `<` comparison and may behave unexpectedly, so ensure a number is passed.","triggerScenarios":"selectBox.selectByIndex(-1); computing an index from a 1-based value without subtracting (index = nth where nth starts at 1); passing a negative constant as a 'no selection' sentinel.","commonSituations":"Off-by-one from 1-based user input or test data; iterating and decrementing past zero; config that uses -1 to mean 'none'.","solutions":["Pass a 0-based non-negative index.","Convert 1-based input: selectByIndex(nth - 1).","Guard: if (index < 0 || !Number.isInteger(index)) throw ... before calling.","Handle a 'none'/sentinel value explicitly rather than forwarding -1."],"exampleFix":"// before\nawait sel.selectByIndex(userData.position); // userData.position is 1-based or -1\n\n// after\nconst idx = Number(userData.position);\nif (!Number.isInteger(idx) || idx < 0) throw new Error(`bad index: ${userData.position}`);\nawait sel.selectByIndex(idx - (isOneBased ? 1 : 0));","handlingStrategy":"validation","validationCode":"function assertValidIndex(index) {\n  if (!Number.isInteger(index) || index < 0) {\n    throw new Error(`index must be a non-negative integer, got ${index}`);\n  }\n}","typeGuard":"function isValidIndex(i) {\n  return Number.isInteger(i) && i >= 0;\n}","tryCatchPattern":"try {\n  await sel.selectByIndex(idx);\n} catch (e) {\n  if (/Index needs to be 0 or any other positive number/.test(e.message)) {\n    await sel.selectByIndex(Math.max(0, idx));\n  } else throw e;\n}","preventionTips":["Convert 1-based input with - 1.","Coerce with Number() and validate Number.isInteger before calling.","Avoid -1 sentinels; handle 'none' explicitly."],"tags":["select","index","validation","bounds"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}