123456789_123456789_123456789_123456789_123456789_

Module: Selenium::WebDriver::SearchContext

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: rb/lib/selenium/webdriver/common/search_context.rb

Constant Summary

  • FINDERS = Internal use only
    # File 'rb/lib/selenium/webdriver/common/search_context.rb', line 24
    {
      class: 'class name',
      class_name: 'class name',
      css: 'css selector',
      id: 'id',
      link: 'link text',
      link_text: 'link text',
      name: 'name',
      partial_link_text: 'partial link text',
      relative: 'relative',
      tag_name: 'tag name',
      xpath: 'xpath'
    }.freeze

Class Attribute Summary

Class Method Summary

Instance Method Summary

Class Attribute Details

.extra_finders (rw)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/search_context.rb', line 39

attr_accessor :extra_finders

Class Method Details

.finders

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/search_context.rb', line 41

def finders
  FINDERS.merge(extra_finders || {})
end

Instance Method Details

#extract_args(args) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/search_context.rb', line 91

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

#find_element(how, what) ⇒ Element #find_element(opts) ⇒ Element

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.

Raises:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/search_context.rb', line 65

def find_element(*args)
  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_elements(*args)

Find all elements matching the given arguments

Raises:

  • (ArgumentError)

See Also:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/search_context.rb', line 80

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