SeleniumHQ/selenium · error · ArgumentError

cannot find elements by #{how.inspect}

Error message

cannot find elements by #{how.inspect}

What it means

Raised by SearchContext#find_elements (plural) when the 'how' (locator strategy key) is not recognized in the FINDERS registry. Functionally identical to the singular find_element version (error 421) but for the multi-element query path. Valid keys are the same: :class, :class_name, :css, :id, :link, :link_text, :name, :partial_link_text, :relative, :tag_name, :xpath.

Source

Thrown at rb/lib/selenium/webdriver/common/search_context.rb:84

        how, what = extract_args(args)

        by = SearchContext.finders[how.to_sym]
        raise ArgumentError, "cannot find element by #{how.inspect}" unless by

        bridge.find_element_by by, what, ref
      end

      #
      # Find all elements matching the given arguments
      #
      # @see SearchContext#find_element
      #

      def find_elements(*args)
        how, what = extract_args(args)

        by = SearchContext.finders[how.to_sym]
        raise ArgumentError, "cannot find elements by #{how.inspect}" unless by

        bridge.find_elements_by by, what, ref
      end

      private

      def extract_args(args)
        case args.size
        when 2
          args
        when 1
          arg = args.first

          unless arg.respond_to?(:shift)
            raise ArgumentError,
                  "expected #{arg.inspect}:#{arg.class} to respond to #shift"
          end

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Use a registered symbol from FINDERS: :class, :class_name, :css, :id, :link, :link_text, :name, :partial_link_text, :relative, :tag_name, :xpath.
  2. Use :link_text or :partial_link_text for link text searches (snake_case).
  3. Register a custom strategy via SearchContext.extra_finders if needed.

Example fix

// before
elements = driver.find_elements(:linkText, 'Read more')

// after
elements = driver.find_elements(:link_text, 'Read more')
Defensive patterns

Strategy: type-guard

Validate before calling

VALID_FINDERS = Selenium::WebDriver::SearchContext.finders.keys

def safe_find_elements(driver, how, what)
  raise ArgumentError, "Unsupported finder: #{how.inspect}" unless VALID_FINDERS.include?(how.to_sym)
  driver.find_elements(how, what)
end

Type guard

def valid_locator_strategy?(how)
  Selenium::WebDriver::SearchContext.finders.key?(how.to_sym)
end

Try / catch

begin
  driver.find_elements(how, what)
rescue ArgumentError => e
  raise unless e.message.include?('cannot find elements by')
  # log and return empty array as graceful degradation
  warn "Invalid locator strategy '#{how}'; returning empty list."
  []
end

Prevention

When it happens

Trigger: Calling find_elements(:linkText, 'Click') (camelCase, invalid) instead of :link_text. Using :partial_link (truncated) instead of :partial_link_text. Passing a strategy key from another binding like :ByXPath or :ClassName.

Common situations: Porting locator code from Java/Python/JS bindings that use different constant names. Autocomplete typos. Using a strategy string from the W3C protocol instead of the Ruby symbol.

Related errors


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