{"record":{"id":"51e8a2c490916b77","repo":"teamcapybara/capybara","slug":"cannot-unselect-option-from-single-select-box","errorCode":null,"errorMessage":"Cannot unselect option from single select box.","messagePattern":"Cannot unselect option from single select box\\.","errorType":"exception","errorClass":"Capybara::UnselectNotAllowed","httpStatus":null,"severity":"error","filePath":"lib/capybara/rack_test/node.rb","lineNumber":56,"sourceCode":"    end\n\n    if radio? then set_radio(value)\n    elsif checkbox? then set_checkbox(value)\n    elsif range? then set_range(value)\n    elsif input_field? then set_input(value)\n    elsif textarea? then native['_capybara_raw_value'] = value.to_s\n    end\n  end\n\n  def select_option\n    return if disabled?\n\n    deselect_options unless select_node.multiple?\n    native['selected'] = 'selected'\n  end\n\n  def unselect_option\n    raise Capybara::UnselectNotAllowed, 'Cannot unselect option from single select box.' unless select_node.multiple?\n\n    native.remove_attribute('selected')\n  end\n\n  def click(keys = [], **options)\n    options.delete(:offset)\n    raise ArgumentError, 'The RackTest driver does not support click options' unless keys.empty? && options.empty?\n\n    if link?\n      follow_link\n    elsif submits?\n      associated_form = form\n      Capybara::RackTest::Form.new(driver, associated_form).submit(self) if associated_form\n    elsif checkable?\n      set(!checked?)\n    elsif tag_name == 'label'\n      click_label\n    elsif (details = native.xpath('.//ancestor-or-self::details').last)","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/teamcapybara/capybara/blob/15b5fdb76e972e9623d5af2123ed3755594f9732/lib/capybara/rack_test/node.rb#L38-L74","documentation":"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.","triggerScenarios":"page.unselect('Blue', from: 'Color') where the Color <select> lacks multiple; find('#colors').find(:option, 'Blue').unselect_option on a single select.","commonSituations":"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.","solutions":["For single selects, choose a different option instead: page.select('Red', from: 'Color').","Add the multiple attribute to the <select> if deselecting is genuinely required.","Guard helper code: only call unselect when find_field('Color').multiple? is true.","If the test intent is 'nothing selected', that state is unreachable in HTML single selects - restate the assertion against the actual UI behavior."],"exampleFix":"# before\npage.unselect('Blue', from: 'Color') # <select> without multiple\n\n# after\npage.select('Red', from: 'Color')","handlingStrategy":"validation","validationCode":"field = find_field('Color')\nraise Capybara::UnselectNotAllowed, 'single select cannot unselect' unless field.multiple?\npage.unselect('Blue', from: 'Color')","typeGuard":"def unselect_allowed?(label)\n  find_field(label).multiple?\nend","tryCatchPattern":null,"preventionTips":["In shared form helpers, branch on find_field(...).multiple? to choose select vs unselect.","Treat single-select 'deselect' intents as 'select a different option' from the start."],"tags":["ruby","capybara","rack-test","forms","select"],"backgroundTag":"invalid-element-state","analyzedSha":"15b5fdb76e972e9623d5af2123ed3755594f9732","analyzedAt":"2026-08-21T16:53:45.588Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}