SeleniumHQ/selenium · error · Error::NoSuchElementError

cannot locate element with index: #{index.inspect}

Error message

cannot locate element with index: #{index.inspect}

What it means

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.

Source

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

          options.each { |e| deselect_option e }
        end

        private

        def select_by_text(text)
          opts = find_by_text text

          return select_options(opts) unless opts.empty?

          raise Error::NoSuchElementError, "cannot locate element with text: #{text.inspect}"
        end

        def select_by_index(index)
          opts = find_by_index index

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

          raise Error::NoSuchElementError, "cannot locate element with index: #{index.inspect}"
        end

        def select_by_value(value)
          opts = find_by_value value

          return select_options(opts) unless opts.empty?

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

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

          opts = find_by_text text

          return deselect_options(opts) unless opts.empty?

          raise Error::NoSuchElementError, "cannot locate element with text: #{text.inspect}"

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Bound-check against select.options.size before selecting by index.
  2. Pass an Integer index (matches the option's zero-based index property).
  3. Prefer select_by(:value) or (:text) when the option set is dynamic.

Example fix

# before
select.select_by(:index, 99)

# after
idx = [desired, select.options.size - 1].min
select.select_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.select_by(:index, idx)

Type guard

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

Try / catch

begin
  select.select_by(:index, idx)
rescue Selenium::WebDriver::Error::NoSuchElementError => e
  raise unless e.message.include?('cannot locate element with index')
  select.select_by(:index, select.options.size - 1)
end

Prevention

When it happens

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

Common situations: 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.

Related errors


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