SeleniumHQ/selenium · error · Error

Select element doesn't contain any option element

Error message

Select element doesn't contain any option element

What it means

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.

Source

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

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

  /**
   *
   * Select option by specific value.

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Wait for at least one option: await driver.wait(driver.until.elementLocated(By.css('select#x option'))).
  2. Trigger the parent dropdown / data fetch first, then poll for options.
  3. Check option count before selecting: if ((await sel.getOptions()).length === 0) handleEmpty();
  4. Add an explicit wait condition rather than a fixed sleep.

Example fix

// before
const sel = new Select(await driver.findElement(By.id('city')));
await sel.selectByIndex(0); // empty -> throws

// after
await driver.wait(async () => (await driver.findElements(By.css('select#city option'))).length > 0, 5000);
const sel = new Select(await driver.findElement(By.id('city')));
await sel.selectByIndex(0);
Defensive patterns

Strategy: retry

Validate before calling

async function hasOptions(sel) {
  return (await sel.getOptions()).length > 0;
}
// await driver.wait(hasOptions.bind(null, sel), 5000) before selectByIndex

Type guard

async function selectHasOptions(sel) {
  return (await sel.getOptions()).length > 0;
}

Try / catch

try {
  await sel.selectByIndex(0);
} catch (e) {
  if (/doesn't contain any option/.test(e.message)) {
    await driver.wait(async () => (await sel.getOptions()).length > 0, 5000);
    await sel.selectByIndex(0);
  } else throw e;
}

Prevention

When it happens

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

Common situations: 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.

Related errors


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