SeleniumHQ/selenium · error · Error

Index needs to be 0 or any other positive number

Error message

Index needs to be 0 or any other positive number

What it means

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.

Source

Thrown at javascript/selenium-webdriver/lib/select.js:184

  /**
   *
   * Select option with specified index.
   *
   * <example>
   <select id="selectbox">
   <option value="1">Option 1</option>
   <option value="2">Option 2</option>
   <option value="3">Option 3</option>
   </select>
   const selectBox = await driver.findElement(By.id("selectbox"));
   await selectObject.selectByIndex(1);
   * </example>
   *
   * @param index
   */
  async selectByIndex(index) {
    if (index < 0) {
      throw new Error('Index needs to be 0 or any other positive number')
    }

    let options = await this.element.findElements(By.tagName('option'))

    if (options.length === 0) {
      throw new Error("Select element doesn't contain any option element")
    }

    if (options.length - 1 < index) {
      throw new Error(
        `Option with index "${index}" not found. Select element only contains ${options.length - 1} option elements`,
      )
    }

    for (let option of options) {
      if ((await option.getAttribute('index')) === index.toString()) {
        await this.setSelected(option)
      }

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Pass a 0-based non-negative index.
  2. Convert 1-based input: selectByIndex(nth - 1).
  3. Guard: if (index < 0 || !Number.isInteger(index)) throw ... before calling.
  4. Handle a 'none'/sentinel value explicitly rather than forwarding -1.

Example fix

// before
await sel.selectByIndex(userData.position); // userData.position is 1-based or -1

// after
const idx = Number(userData.position);
if (!Number.isInteger(idx) || idx < 0) throw new Error(`bad index: ${userData.position}`);
await sel.selectByIndex(idx - (isOneBased ? 1 : 0));
Defensive patterns

Strategy: validation

Validate before calling

function assertValidIndex(index) {
  if (!Number.isInteger(index) || index < 0) {
    throw new Error(`index must be a non-negative integer, got ${index}`);
  }
}

Type guard

function isValidIndex(i) {
  return Number.isInteger(i) && i >= 0;
}

Try / catch

try {
  await sel.selectByIndex(idx);
} catch (e) {
  if (/Index needs to be 0 or any other positive number/.test(e.message)) {
    await sel.selectByIndex(Math.max(0, idx));
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: Off-by-one from 1-based user input or test data; iterating and decrementing past zero; config that uses -1 to mean 'none'.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/a9882428a65362cc. Report an issue: GitHub.