SeleniumHQ/selenium · error · Error::NoSuchElementError

cannot locate option with index: #{index}

Error message

cannot locate option with index: #{index}

What it means

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.

Source

Thrown at rb/lib/selenium/webdriver/support/select.rb:214

        def deselect_by_value(value)
          raise Error::UnsupportedOperationError, 'you may only deselect option of a multi-select' unless multiple?

          opts = find_by_value value

          return deselect_options(opts) unless opts.empty?

          raise Error::NoSuchElementError, "cannot locate option with value: #{value.inspect}"
        end

        def deselect_by_index(index)
          raise Error::UnsupportedOperationError, 'you may only deselect option of a multi-select' unless multiple?

          opts = find_by_index index

          return deselect_option(opts.first) unless opts.empty?

          raise Error::NoSuchElementError, "cannot locate option with index: #{index}"
        end

        def select_option(option)
          raise Error::UnsupportedOperationError, 'You may not select a disabled option' unless option.enabled?

          option.click unless option.selected?
        end

        def deselect_option(option)
          option.click if option.selected?
        end

        def select_options(opts)
          if multiple?
            opts.each { |o| select_option o }
          else
            select_option opts.first
          end

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Bound-check against select.options.size before deselecting by index.
  2. Pass an Integer index (zero-based, matching the option index property).
  3. Prefer deselect_by(:value) or (:text) for dynamic option sets.

Example fix

# before
select.deselect_by(:index, 99)

# after
idx = [desired, select.options.size - 1].min
select.deselect_by(:index, idx)
Defensive patterns

Strategy: validation

Validate before calling

idx = Integer(desired)
raise ArgumentError, 'index out of range' unless idx.between?(0, select.options.size - 1)
select.deselect_by(:index, idx)

Type guard

def valid_deselect_index?(select, idx)
  idx.is_a?(Integer) && idx.between?(0, select.options.size - 1)
end

Try / catch

begin
  select.deselect_by(:index, idx)
rescue Selenium::WebDriver::Error::NoSuchElementError => e
  raise unless e.message.include?('cannot locate option with index')
  # index out of range on multi-select — nothing to deselect
end

Prevention

When it happens

Trigger: 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.

Common situations: Index beyond options count; passing a String index instead of Integer; assuming one-based indexing (DOM index property is zero-based).

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/f3d7866e1bfa83be. Report an issue: GitHub.