# File lib/celerity/element_locator.rb, line 23
    def find_by_conditions(conditions) # TODO: refactor without performance hit
      return nil unless @object # probably means we're on a TextPage (content-type is "text/plain")

      @condition_idents = []
      attributes = Hash.new { |h, k| h[k] = [] }
      index = 0 # by default, return the first matching element
      text = nil

      conditions.each do |how, what|
        case how
        when :object
          unless what.is_a?(HtmlUnit::Html::HtmlElement) || what.nil?
            raise ArgumentError, "expected an HtmlUnit::Html::HtmlElement subclass, got #{what.inspect}:#{what.class}"
          end
          return what
        when :xpath
          return find_by_xpath(what)
        when :label
          return find_by_label(what) unless @attributes.include?(:label)
        when :class_name
          how = :class
        when :url
          how = :href
        when :caption
          how = :text
        end

        if how == :id && conditions.size == 1
          return find_by_id(what)
        elsif @attributes.include?(how = how.to_sym)
          attributes[how] << what
        elsif how == :index
          index = what.to_i - Celerity.index_offset
        elsif how == :text
          text = what
        else
          raise MissingWayOfFindingObjectException, "No how #{how.inspect}"
        end
      end

      @idents.each do |ident|
        merged = attributes.merge(ident.attributes) { |key, v1, v2| v1 | v2 }
        id = Identifier.new(ident.tag, merged)
        id.text = ident.text || text # «original» identifier takes precedence for :text
        @condition_idents << id
      end

      if index == 0
        element_by_idents(@condition_idents)
      else
        elements_by_idents(@condition_idents)[index]
      end

    rescue HtmlUnit::ElementNotFoundException
      nil # for rcov
    end