module Selenium::WebDriver::SearchContext

Constants

FINDERS

@api private

Public Instance Methods

find_element(*args) click to toggle source

Find the first element matching the given arguments

When using Element#find_element with :xpath, be aware that webdriver follows standard conventions: a search prefixed with “//” will search the entire document, not just the children of this current node. Use “.//” to limit your search to the children of the receiving Element.

@overload find_element(how, what)

@param [Symbol, String] how The method to find the element by
@param [String] what The locator to use

@overload find_element(opts)

@param [Hash] opts Find options
@option opts [Symbol] :how Key named after the method to find the element by, containing the locator

@return [Element]

@raise [Error::NoSuchElementError] if the element doesn't exist

# File lib/selenium/webdriver/common/search_context.rb, line 57
def find_element(*args)
  how, what = extract_args(args)

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

  bridge.find_element_by by, what, ref
rescue Selenium::WebDriver::Error::TimeoutError
  # Implicit Wait times out in Edge
  raise Selenium::WebDriver::Error::NoSuchElementError
end
find_elements(*args) click to toggle source

Find all elements matching the given arguments

@see SearchContext#find_element

# File lib/selenium/webdriver/common/search_context.rb, line 75
def find_elements(*args)
  how, what = extract_args(args)

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

  bridge.find_elements_by by, what, ref
rescue Selenium::WebDriver::Error::TimeoutError
  # Implicit Wait times out in Edge
  []
end

Private Instance Methods

extract_args(args) click to toggle source
# File lib/selenium/webdriver/common/search_context.rb, line 89
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

    # this will be a single-entry hash, so use #shift over #first or #[]
    arr = arg.dup.shift
    raise ArgumentError, "expected #{arr.inspect} to have 2 elements" unless arr.size == 2

    arr
  else
    raise ArgumentError, "wrong number of arguments (#{args.size} for 2)"
  end
end