SeleniumHQ/selenium · error · Error

Option with index "${index}" not found. Select element only

Error message

Option with index "${index}" not found. Select element only contains ${options.length - 1} option elements

What it means

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.

Source

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

   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)
      }
    }
  }

  /**
   *
   * Select option by specific value.
   *
   * <example>
   <select id="selectbox">
   <option value="1">Option 1</option>

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Derive the index dynamically from the real option count: const n = (await sel.getOptions()).length; selectByIndex(Math.min(wanted, n - 1)).
  2. Prefer selecting by visible text or value which is resilient to reordering/count changes.
  3. Clamp and warn: if (index > n - 1) { index = n - 1; log.warn(...); }.
  4. Fixtures: keep test option data in sync with the page or assert the expected count first.

Example fix

// before
await sel.selectByIndex(5); // page now has only 4 options

// after
const count = (await sel.getOptions()).length;
await sel.selectByIndex(Math.min(5, count - 1));
Defensive patterns

Strategy: validation

Validate before calling

async function safeSelectByIndex(sel, idx) {
  const n = (await sel.getOptions()).length;
  if (n === 0) throw new Error('no options');
  if (idx > n - 1) idx = n - 1;
  await sel.selectByIndex(idx);
}

Type guard

async function indexInRange(sel, idx) {
  const n = (await sel.getOptions()).length;
  return idx >= 0 && idx < n;
}

Try / catch

try {
  await sel.selectByIndex(idx);
} catch (e) {
  if (/Option with index/.test(e.message)) {
    const n = (await sel.getOptions()).length;
    await sel.selectByIndex(n - 1);
  } else throw e;
}

Prevention

When it happens

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

Common situations: Page redesign reduces the number of options; locale variants with fewer options; stale test data; assuming length == last index.

Related errors


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