{"record":{"id":"f3d7866e1bfa83be","repo":"SeleniumHQ/selenium","slug":"cannot-locate-option-with-index-index","errorCode":null,"errorMessage":"cannot locate option with index: #{index}","messagePattern":"cannot locate option with index: #(.+?)","errorType":"exception","errorClass":"Error::NoSuchElementError","httpStatus":null,"severity":"error","filePath":"rb/lib/selenium/webdriver/support/select.rb","lineNumber":214,"sourceCode":"\n        def deselect_by_value(value)\n          raise Error::UnsupportedOperationError, 'you may only deselect option of a multi-select' unless multiple?\n\n          opts = find_by_value value\n\n          return deselect_options(opts) unless opts.empty?\n\n          raise Error::NoSuchElementError, \"cannot locate option with value: #{value.inspect}\"\n        end\n\n        def deselect_by_index(index)\n          raise Error::UnsupportedOperationError, 'you may only deselect option of a multi-select' unless multiple?\n\n          opts = find_by_index index\n\n          return deselect_option(opts.first) unless opts.empty?\n\n          raise Error::NoSuchElementError, \"cannot locate option with index: #{index}\"\n        end\n\n        def select_option(option)\n          raise Error::UnsupportedOperationError, 'You may not select a disabled option' unless option.enabled?\n\n          option.click unless option.selected?\n        end\n\n        def deselect_option(option)\n          option.click if option.selected?\n        end\n\n        def select_options(opts)\n          if multiple?\n            opts.each { |o| select_option o }\n          else\n            select_option opts.first\n          end","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/rb/lib/selenium/webdriver/support/select.rb#L196-L232","documentation":"In private deselect_by_index, after passing the multi-select guard, if no option's :index property equals the given index the binding raises Error::NoSuchElementError. Note the message uses #{index} (no .inspect), so a non-Integer index renders plainly. The multi-select guard (498) fires first; this fires on multi-selects where the index is out of range or wrong type.","triggerScenarios":"On a multi-select, calling deselect_by(:index, n) where n matches no option's :index property — index out of range, negative, or a non-Integer type that never equals the Integer index property.","commonSituations":"Index beyond options count; passing a String index instead of Integer; assuming one-based indexing (DOM index property is zero-based).","solutions":["Bound-check against select.options.size before deselecting by index.","Pass an Integer index (zero-based, matching the option index property).","Prefer deselect_by(:value) or (:text) for dynamic option sets."],"exampleFix":"# before\nselect.deselect_by(:index, 99)\n\n# after\nidx = [desired, select.options.size - 1].min\nselect.deselect_by(:index, idx)","handlingStrategy":"validation","validationCode":"idx = Integer(desired)\nraise ArgumentError, 'index out of range' unless idx.between?(0, select.options.size - 1)\nselect.deselect_by(:index, idx)","typeGuard":"def valid_deselect_index?(select, idx)\n  idx.is_a?(Integer) && idx.between?(0, select.options.size - 1)\nend","tryCatchPattern":"begin\n  select.deselect_by(:index, idx)\nrescue Selenium::WebDriver::Error::NoSuchElementError => e\n  raise unless e.message.include?('cannot locate option with index')\n  # index out of range on multi-select — nothing to deselect\nend","preventionTips":["Bound-check index against select.options.size.","Pass an Integer index (zero-based).","Use deselect_by(:value) for dynamic option sets."],"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"}