class Watir::ElementCollection

Base class for element collections.

Public Class Methods

new(parent, selector) click to toggle source
# File lib/watir-webdriver/element_collection.rb, line 10
def initialize(parent, selector)
  @parent   = parent
  @selector = selector
end

Public Instance Methods

[](idx) click to toggle source

Get the element at the given index.

Also note that because of Watir's lazy loading, this will return an Element instance even if the index is out of bounds.

@param [Fixnum] idx Index of wanted element, 0-indexed @return [Watir::Element] Returns an instance of a Watir::Element subclass

# File lib/watir-webdriver/element_collection.rb, line 52
def [](idx)
  to_a[idx] || element_class.new(@parent, @selector.merge(index: idx))
end
each(&blk) click to toggle source

Yields each element in collection.

@example

divs = browser.divs(class: 'kls')
divs.each do |div|
  puts div.text
end

@yieldparam [Watir::Element] element Iterate through the elements in this collection.

# File lib/watir-webdriver/element_collection.rb, line 27
def each(&blk)
  to_a.each(&blk)
end
first() click to toggle source

First element of this collection

@return [Watir::Element] Returns an instance of a Watir::Element subclass

# File lib/watir-webdriver/element_collection.rb, line 62
def first
  self[0]
end
last() click to toggle source

Last element of the collection

@return [Watir::Element] Returns an instance of a Watir::Element subclass

# File lib/watir-webdriver/element_collection.rb, line 72
def last
  self[-1]
end
length() click to toggle source

Returns number of elements in collection.

@return [Fixnum]

# File lib/watir-webdriver/element_collection.rb, line 37
def length
  elements.length
end
Also aliased as: size
size()
Alias for: length
to_a() click to toggle source

This collection as an Array.

@return [Array<Watir::Element>]

# File lib/watir-webdriver/element_collection.rb, line 82
def to_a
  # TODO: optimize - lazy element_class instance?
  @to_a ||= elements.map { |e| element_class.new(@parent, element: e) }
end

Private Instance Methods

element_class() click to toggle source
# File lib/watir-webdriver/element_collection.rb, line 121
def element_class
  Kernel.const_get(self.class.name.sub(/Collection$/, ''))
end
element_class_name() click to toggle source
# File lib/watir-webdriver/element_collection.rb, line 117
def element_class_name
  element_class.to_s.split('::').last
end
element_validator_class() click to toggle source
# File lib/watir-webdriver/element_collection.rb, line 105
def element_validator_class
  Kernel.const_get("#{Watir.locator_namespace}::#{element_class_name}::Validator")
rescue NameError
  Kernel.const_get("#{Watir.locator_namespace}::Element::Validator")
end
elements() click to toggle source
# File lib/watir-webdriver/element_collection.rb, line 89
def elements
  @parent.is_a?(IFrame) ? @parent.switch_to! : @parent.send(:assert_exists)

  element_validator = element_validator_class.new
  selector_builder = selector_builder_class.new(@parent, @selector, element_class.attribute_list)
  locator = locator_class.new(@parent, @selector, selector_builder, element_validator)

  @elements ||= locator.locate_all
end
locator_class() click to toggle source
# File lib/watir-webdriver/element_collection.rb, line 99
def locator_class
  Kernel.const_get("#{Watir.locator_namespace}::#{element_class_name}::Locator")
rescue NameError
  Kernel.const_get("#{Watir.locator_namespace}::Element::Locator")
end
selector_builder_class() click to toggle source
# File lib/watir-webdriver/element_collection.rb, line 111
def selector_builder_class
  Kernel.const_get("#{Watir.locator_namespace}::#{element_class_name}::SelectorBuilder")
rescue NameError
  Kernel.const_get("#{Watir.locator_namespace}::Element::SelectorBuilder")
end