SeleniumHQ/selenium · error · NoSuchElementException
Could not locate element with index {index}
Error message
Could not locate element with index {index} What it means
`select_by_index()` matches an option whose DOM `index` attribute equals the given integer. If no option has that index attribute it raises `NoSuchElementException`. Note it matches the HTML `index` attribute, not the list position.
Source
Thrown at py/selenium/webdriver/support/select.py:101
matched = True
if not matched:
raise NoSuchElementException(f"Cannot locate option with value: {value}")
def select_by_index(self, index: int) -> None:
"""Select the option at the given index by examining the "index" attribute.
Args:
index: The option at this index will be selected
Raises:
NoSuchElementException: If there is no option with specified index in SELECT
"""
match = str(index)
for opt in self.options:
if opt.get_attribute("index") == match:
self._set_selected(opt)
return
raise NoSuchElementException(f"Could not locate element with index {index}")
def select_by_visible_text(self, text: str) -> None:
"""Select all options that display text matching the argument.
Example:
When given "Bar" this would select an option like:
`<option value="foo">Bar</option>`
Args:
text: The visible text to match against
Raises:
NoSuchElementException: If there is no option with specified text in SELECT
"""
xpath = f".//option[normalize-space(.) = {self._escape_string(text)}]"
opts = self._el.find_elements(By.XPATH, xpath)
matched = FalseView on GitHub (pinned to aa36b38e69)
Solutions
- Check the option count first: `len(select.options)` and ensure index is in range
- Prefer `select_by_value` or `select_by_visible_text` for stability across dynamic lists
- Catch NoSuchElementException and handle gracefully
Example fix
// before
select.select_by_index(5)
// after
if len(select.options) > 5:
select.select_by_index(5) Defensive patterns
Strategy: try-catch
Validate before calling
if 0 <= index < len(select.options):
select.select_by_index(index) Type guard
def index_in_range(select_obj, index) -> bool:
return 0 <= index < len(select_obj.options) Try / catch
from selenium.common.exceptions import NoSuchElementException
try:
select.select_by_index(index)
except NoSuchElementException:
# index not present
pass Prevention
- Check len(select.options) before indexing
- Prefer select_by_value for dynamic option lists
- Remember the index attribute may not equal list position
When it happens
Trigger: Calling `select.select_by_index(5)` when fewer options exist, or when the option's `index` attribute does not correspond to its position. Passing a negative index or one beyond the option count.
Common situations: Assuming index equals list position (the `index` attribute is assigned by the browser and usually aligns, but dynamic option lists can shift); requesting an index after options were removed; off-by-one from zero- vs one-based thinking.
Related errors
- Cannot locate option with value: {value}
- Could not locate element with visible text: {text}
- Could not locate element with value: {value}
- Select only works on <select> elements, not on {webelement.t
- No options are selected
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/ad455598a2c5944d.
Report an issue: GitHub.