{"record":{"id":"ded7a6cb7e15aea2","repo":"SeleniumHQ/selenium","slug":"cannot-locate-element-with-index-index-inspect","errorCode":null,"errorMessage":"cannot locate element with index: #{index.inspect}","messagePattern":"cannot locate element with index: #(.+?)","errorType":"exception","errorClass":"Error::NoSuchElementError","httpStatus":null,"severity":"error","filePath":"rb/lib/selenium/webdriver/support/select.rb","lineNumber":176,"sourceCode":"          options.each { |e| deselect_option e }\n        end\n\n        private\n\n        def select_by_text(text)\n          opts = find_by_text text\n\n          return select_options(opts) unless opts.empty?\n\n          raise Error::NoSuchElementError, \"cannot locate element with text: #{text.inspect}\"\n        end\n\n        def select_by_index(index)\n          opts = find_by_index index\n\n          return select_option(opts.first) unless opts.empty?\n\n          raise Error::NoSuchElementError, \"cannot locate element with index: #{index.inspect}\"\n        end\n\n        def select_by_value(value)\n          opts = find_by_value value\n\n          return select_options(opts) unless opts.empty?\n\n          raise Error::NoSuchElementError, \"cannot locate option with value: #{value.inspect}\"\n        end\n\n        def deselect_by_text(text)\n          raise Error::UnsupportedOperationError, 'you may only deselect option of a multi-select' unless multiple?\n\n          opts = find_by_text text\n\n          return deselect_options(opts) unless opts.empty?\n\n          raise Error::NoSuchElementError, \"cannot locate element with text: #{text.inspect}\"","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/rb/lib/selenium/webdriver/support/select.rb#L158-L194","documentation":"In private select_by_index, the binding selects options whose property(:index) equals the given index; if none match it raises Error::NoSuchElementError. Note index is matched against the option's DOM index property, not an array position, so off-by-one or non-integer indexes fail here.","triggerScenarios":"Calling select.select_by(:index, n) where n does not equal any option's :index property — e.g. an index out of range, a negative index, or a non-Integer passed as what (since property(:index) returns an Integer).","commonSituations":"Index larger than the number of options; assuming zero-based vs one-based (it matches the DOM index property which is zero-based); passing a String '2' instead of Integer 2.","solutions":["Bound-check against select.options.size before selecting by index.","Pass an Integer index (matches the option's zero-based index property).","Prefer select_by(:value) or (:text) when the option set is dynamic."],"exampleFix":"# before\nselect.select_by(:index, 99)\n\n# after\nidx = [desired, select.options.size - 1].min\nselect.select_by(:index, idx)","handlingStrategy":"validation","validationCode":"idx = Integer(desired)\nraise ArgumentError, 'index out of range' unless idx.between?(0, select.options.size - 1)\nselect.select_by(:index, idx)","typeGuard":"def valid_select_index?(select, idx)\n  idx.is_a?(Integer) && idx.between?(0, select.options.size - 1)\nend","tryCatchPattern":"begin\n  select.select_by(:index, idx)\nrescue Selenium::WebDriver::Error::NoSuchElementError => e\n  raise unless e.message.include?('cannot locate element with index')\n  select.select_by(:index, select.options.size - 1)\nend","preventionTips":["Bound-check index against select.options.size.","Pass an Integer (zero-based, matching the DOM index property).","Use select_by(:value) for dynamic option lists."],"tags":["select","no-such-element","ruby"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}