teamcapybara/capybara · error · Capybara::UnselectNotAllowed

Cannot unselect option from single select box.

Error message

Cannot unselect option from single select box.

What it means

In HTML a single <select> (no multiple attribute) must always have exactly one option selected, so 'unselect' has no meaning there. rack_test therefore raises Capybara::UnselectNotAllowed from Node#unselect_option when the owning select is not multiple. Unselecting is only supported for multi-select boxes.

Source

Thrown at lib/capybara/rack_test/node.rb:56

    end

    if radio? then set_radio(value)
    elsif checkbox? then set_checkbox(value)
    elsif range? then set_range(value)
    elsif input_field? then set_input(value)
    elsif textarea? then native['_capybara_raw_value'] = value.to_s
    end
  end

  def select_option
    return if disabled?

    deselect_options unless select_node.multiple?
    native['selected'] = 'selected'
  end

  def unselect_option
    raise Capybara::UnselectNotAllowed, 'Cannot unselect option from single select box.' unless select_node.multiple?

    native.remove_attribute('selected')
  end

  def click(keys = [], **options)
    options.delete(:offset)
    raise ArgumentError, 'The RackTest driver does not support click options' unless keys.empty? && options.empty?

    if link?
      follow_link
    elsif submits?
      associated_form = form
      Capybara::RackTest::Form.new(driver, associated_form).submit(self) if associated_form
    elsif checkable?
      set(!checked?)
    elsif tag_name == 'label'
      click_label
    elsif (details = native.xpath('.//ancestor-or-self::details').last)

View on GitHub (pinned to 15b5fdb76e)

Solutions

  1. For single selects, choose a different option instead: page.select('Red', from: 'Color').
  2. Add the multiple attribute to the <select> if deselecting is genuinely required.
  3. Guard helper code: only call unselect when find_field('Color').multiple? is true.
  4. If the test intent is 'nothing selected', that state is unreachable in HTML single selects - restate the assertion against the actual UI behavior.

Example fix

# before
page.unselect('Blue', from: 'Color') # <select> without multiple

# after
page.select('Red', from: 'Color')
Defensive patterns

Strategy: validation

Validate before calling

field = find_field('Color')
raise Capybara::UnselectNotAllowed, 'single select cannot unselect' unless field.multiple?
page.unselect('Blue', from: 'Color')

Type guard

def unselect_allowed?(label)
  find_field(label).multiple?
end

Prevention

When it happens

Trigger: page.unselect('Blue', from: 'Color') where the Color <select> lacks multiple; find('#colors').find(:option, 'Blue').unselect_option on a single select.

Common situations: Markup changed from multi-select to a dropdown but specs still unselect; shared helpers written against a multi-select reused on single selects; porting Watir/Selenium scripts that call deselect unconditionally.

Related errors


AI-assisted analysis of teamcapybara/capybara@15b5fdb76e (2026-08-21). Data as JSON: /api/errors/51e8a2c490916b77. Report an issue: GitHub.