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
- Derive the index dynamically from the real option count: const n = (await sel.getOptions()).length; selectByIndex(Math.min(wanted, n - 1)).
- Prefer selecting by visible text or value which is resilient to reordering/count changes.
- Clamp and warn: if (index > n - 1) { index = n - 1; log.warn(...); }.
- 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
- Derive indices from the live option count.
- Prefer value/text selection over positional indices.
- Keep test fixtures in sync with the page or assert the count first.
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
- Index needs to be 0 or any other positive number
- Element must not be null. Please provide a valid <select> el
- Select only works on <select> elements
- Select element doesn't contain any option element
- Cannot locate option with value: ${value}
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/cb57bd9387f4848d.
Report an issue: GitHub.