SeleniumHQ/selenium · error · Error::NoSuchElementError

no options are selected

Error message

no options are selected

What it means

Support::Select#first_selected_option returns the first option whose selected? is true; if none is selected it raises Error::NoSuchElementError. This represents the W3C 'no selected option' state on the page rather than a code bug, so it surfaces as the standard WebDriver element-not-found error type.

Source

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

        # @return [Array<Element>]
        #

        def selected_options
          options.select(&:selected?)
        end

        #
        # Get the first selected option in this select element
        #
        # @raise [Error::NoSuchElementError] if no options are selected
        # @return [Element]
        #

        def first_selected_option
          option = options.find(&:selected?)
          return option if option

          raise Error::NoSuchElementError, 'no options are selected'
        end

        #
        # Select options by visible text, index or value.
        #
        # When selecting by :text, selects options that display text matching the argument. That is, when given "Bar" this
        # would select an option like:
        #
        #     <option value="foo">Bar</option>
        #
        # When selecting by :value, selects all options that have a value matching the argument. That is, when given "foo" this
        # would select an option like:
        #
        #     <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.
        #

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Check select.selected_options.any? (or select.selected_options.empty?) before calling first_selected_option.
  2. Confirm the select actually has a default selected <option> in the HTML (an option with the 'selected' attribute).
  3. Handle the NoSuchElementError as the expected 'nothing selected' case in your assertion.

Example fix

# before
chosen = select.first_selected_option

# after
if select.selected_options.empty?
  chosen = nil
else
  chosen = select.first_selected_option
end
Defensive patterns

Strategy: validation

Validate before calling

if select.selected_options.empty?
  chosen = nil
else
  chosen = select.first_selected_option
end

Type guard

def has_selected_option?(select)
  select.selected_options.any?
end

Try / catch

begin
  select.first_selected_option
rescue Selenium::WebDriver::Error::NoSuchElementError
  # no option selected — handle accordingly
  nil
end

Prevention

When it happens

Trigger: Calling select.first_selected_option on a <select> where zero <option> elements are selected (common for single-selects with no default selection, or after deselect_all on a multi-select).

Common situations: Asserting the default selection on a fresh single-select that has no pre-selected option; reading state after programmatic deselection; page load race where the select exists but options haven't been populated/selected yet.

Related errors


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