SeleniumHQ/selenium · error · ArgumentError

can't select options by #{how.inspect}

Error message

can't select options by #{how.inspect}

What it means

Support::Select#select_by switches on the how argument and accepts only :text, :index, or :value. Any other Symbol/value falls to else and raises ArgumentError. This is a misuse of the API surface rather than a page-state problem.

Source

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

        #     <option value="foo">Bar</option>
        #
        # When selecting by :index, selects the option at the given index. This is done by examining the "index" attribute of an
        # element, and not merely by counting.
        #
        # @param [:text, :index, :value] how How to find the option
        # @param [String] what What value to find the option by.
        #

        def select_by(how, what)
          case how
          when :text
            select_by_text what
          when :index
            select_by_index what
          when :value
            select_by_value what
          else
            raise ArgumentError, "can't select options by #{how.inspect}"
          end
        end

        #
        # Deselect options by visible text, index or value.
        #
        # @param [:text, :index, :value] how How to find the option
        # @param [String] what What value to find the option by.
        # @raise [Error::UnsupportedOperationError] if the element does not support multiple selections.
        #
        # @see Select#select_by
        #

        def deselect_by(how, what)
          case how
          when :text
            deselect_by_text what
          when :value

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Use one of the three valid selectors: select.select_by(:text, 'Bar'), select.select_by(:value, 'foo'), or select.select_by(:index, 0).
  2. Pass Symbols not Strings for how.
  3. If you need css/xpath/id selection, find the option element directly and click it instead of using select_by.

Example fix

# before
select.select_by('text', 'Bar')

# after
select.select_by(:text, 'Bar')
Defensive patterns

Strategy: validation

Validate before calling

VALID_HOW = %i[text index value].freeze
raise ArgumentError, "bad how #{how}" unless VALID_HOW.include?(how)
select.select_by(how, what)

Type guard

def valid_select_how?(how)
  %i[text index value].include?(how)
end

Try / catch

begin
  select.select_by(how, what)
rescue ArgumentError => e
  raise unless e.message.include?("can't select options")
  select.select_by(:text, what.to_s)
end

Prevention

When it happens

Trigger: Calling select.select_by(:id, 'x'), select.select_by('text', 'x') (String instead of Symbol — 'text' != :text), select.select_by(:name, 'x'), or passing any key not in {text, index, value}.

Common situations: Using a find-strategy Symbol from driver.find_element (e.g. :id, :css, :name) that is invalid for Select; passing a String 'text' instead of the Symbol :text; copy-pasting a different library's locator API.

Related errors


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