{"record":{"id":"ad455598a2c5944d","repo":"SeleniumHQ/selenium","slug":"could-not-locate-element-with-index-index","errorCode":null,"errorMessage":"Could not locate element with index {index}","messagePattern":"Could not locate element with index (.+?)","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"py/selenium/webdriver/support/select.py","lineNumber":101,"sourceCode":"            matched = True\n        if not matched:\n            raise NoSuchElementException(f\"Cannot locate option with value: {value}\")\n\n    def select_by_index(self, index: int) -> None:\n        \"\"\"Select the option at the given index by examining the \"index\" attribute.\n\n        Args:\n            index: The option at this index will be selected\n\n        Raises:\n            NoSuchElementException: If there is no option with specified index in SELECT\n        \"\"\"\n        match = str(index)\n        for opt in self.options:\n            if opt.get_attribute(\"index\") == match:\n                self._set_selected(opt)\n                return\n        raise NoSuchElementException(f\"Could not locate element with index {index}\")\n\n    def select_by_visible_text(self, text: str) -> None:\n        \"\"\"Select all options that display text matching the argument.\n\n        Example:\n            When given \"Bar\" this would select an option like:\n\n            `<option value=\"foo\">Bar</option>`\n\n        Args:\n            text: The visible text to match against\n\n        Raises:\n            NoSuchElementException: If there is no option with specified text in SELECT\n        \"\"\"\n        xpath = f\".//option[normalize-space(.) = {self._escape_string(text)}]\"\n        opts = self._el.find_elements(By.XPATH, xpath)\n        matched = False","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/support/select.py#L83-L119","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nselect.select_by_index(5)\n\n// after\nif len(select.options) > 5:\n    select.select_by_index(5)","handlingStrategy":"try-catch","validationCode":"if 0 <= index < len(select.options):\n    select.select_by_index(index)","typeGuard":"def index_in_range(select_obj, index) -> bool:\n    return 0 <= index < len(select_obj.options)","tryCatchPattern":"from selenium.common.exceptions import NoSuchElementException\n\ntry:\n    select.select_by_index(index)\nexcept NoSuchElementException:\n    # index not present\n    pass","preventionTips":["Check len(select.options) before indexing","Prefer select_by_value for dynamic option lists","Remember the index attribute may not equal list position"],"tags":["select","element-not-found","selenium-python"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}