SeleniumHQ/selenium · error · ArgumentError

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

Error message

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

What it means

Support::Select#deselect_by mirrors select_by: it switches on how and accepts only :text, :value, :index. Any other value raises ArgumentError. Like 487 this is an API-misuse error, distinct from the UnsupportedOperationError raised when deselect is attempted on a single-select.

Source

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

        # 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
            deselect_by_value what
          when :index
            deselect_by_index what
          else
            raise ArgumentError, "can't deselect options by #{how.inspect}"
          end
        end

        #
        # Select all unselected options. Only valid if the element supports multiple selections.
        #
        # @raise [Error::UnsupportedOperationError] if the element does not support multiple selections.
        #

        def select_all
          raise Error::UnsupportedOperationError, 'you may only select all options of a multi-select' unless multiple?

          options.each { |e| select_option e }
        end

        #
        # Deselect all selected options. Only valid if the element supports multiple selections.
        #

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Use one of :text, :value, or :index for how.
  2. Pass a Symbol, not a String.
  3. For other locator strategies, find and click the option element directly to toggle it.

Example fix

# before
select.deselect_by('value', 'foo')

# after
select.deselect_by(:value, 'foo')
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling select.deselect_by(:id, 'x'), select.deselect_by(:name, 'x'), select.deselect_by('value', 'x') (String key), or any how outside {text, value, index}.

Common situations: Reusing locator symbols from elsewhere in the test (css, id, name); passing a String instead of Symbol; assuming deselect supports the same locator types as driver.find_element.

Related errors


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