Class: Watir::ElementCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/watir-webdriver/element_collection.rb

Overview

Base class for element collections.

Direct Known Subclasses

AnchorCollection, AnimateCollection, AnimateMotionCollection, AnimateTransformCollection, AnimationCollection, AppletCollection, AreaCollection, AudioCollection, BRCollection, BaseCollection, BodyCollection, ButtonCollection, CanvasCollection, CircleCollection, CursorCollection, DListCollection, DataCollection, DataListCollection, DefsCollection, DescCollection, DetailsCollection, DialogCollection, DirectoryCollection, DivCollection, EllipseCollection, EmbedCollection, FieldSetCollection, FontCollection, ForeignObjectCollection, FormCollection, FrameSetCollection, GCollection, GeometryCollection, GradientCollection, GraphicsCollection, HRCollection, HTMLElementCollection, HeadCollection, HeadingCollection, HtmlCollection, IFrameCollection, ImageCollection, InputCollection, KeygenCollection, LICollection, LabelCollection, LegendCollection, LineCollection, LinearGradientCollection, MPathCollection, MapCollection, MarkerCollection, MarqueeCollection, MediaCollection, MenuCollection, MenuItemCollection, MeshGradientCollection, MeshPatchCollection, MeshRowCollection, MetaCollection, MetadataCollection, MeterCollection, ModCollection, OListCollection, ObjectCollection, OptGroupCollection, OptionCollection, OutputCollection, ParagraphCollection, ParamCollection, PathCollection, PatternCollection, PictureCollection, PolygonCollection, PolylineCollection, PreCollection, ProgressCollection, QuoteCollection, RadialGradientCollection, RectCollection, SVGCollection, SVGElementCollection, ScriptCollection, SelectCollection, SetCollection, SourceCollection, SpanCollection, StopCollection, StyleCollection, SwitchCollection, SymbolCollection, TSpanCollection, TableCaptionCollection, TableCellCollection, TableColCollection, TableCollection, TableDataCellCollection, TableHeaderCellCollection, TableRowCollection, TableSectionCollection, TemplateCollection, TextAreaCollection, TextContentCollection, TextPathCollection, TextPositioningCollection, TimeCollection, TitleCollection, TrackCollection, UListCollection, UnknownCollection, UseCollection, VideoCollection, ViewCollection

Instance Method Summary collapse

Constructor Details

#initialize(parent, selector) ⇒ ElementCollection

Returns a new instance of ElementCollection.



10
11
12
13
# File 'lib/watir-webdriver/element_collection.rb', line 10

def initialize(parent, selector)
  @parent   = parent
  @selector = selector
end

Instance Method Details

#[](idx) ⇒ Watir::Element

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.

Parameters:

  • idx (Fixnum)

    Index of wanted element, 0-indexed

Returns:

  • (Watir::Element)

    Returns an instance of a Watir::Element subclass



52
53
54
# File 'lib/watir-webdriver/element_collection.rb', line 52

def [](idx)
  to_a[idx] || element_class.new(@parent, @selector.merge(index: idx))
end

#each {|element| ... } ⇒ Object

Yields each element in collection.

Examples:

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

Yield Parameters:

  • element (Watir::Element)

    Iterate through the elements in this collection.



27
28
29
# File 'lib/watir-webdriver/element_collection.rb', line 27

def each(&blk)
  to_a.each(&blk)
end

#firstWatir::Element

First element of this collection

Returns:

  • (Watir::Element)

    Returns an instance of a Watir::Element subclass



62
63
64
# File 'lib/watir-webdriver/element_collection.rb', line 62

def first
  self[0]
end

#lastWatir::Element

Last element of the collection

Returns:

  • (Watir::Element)

    Returns an instance of a Watir::Element subclass



72
73
74
# File 'lib/watir-webdriver/element_collection.rb', line 72

def last
  self[-1]
end

#lengthFixnum Also known as: size

Returns number of elements in collection.

Returns:

  • (Fixnum)


37
38
39
# File 'lib/watir-webdriver/element_collection.rb', line 37

def length
  elements.length
end

#to_aArray<Watir::Element>

This collection as an Array.

Returns:



82
83
84
85
# 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