Class: Selenium::WebDriver::Element

Inherits:
Object
  • Object
show all
Includes:
SearchContext
Defined in:
lib/selenium/webdriver/common/element.rb

Constant Summary

Constants included from SearchContext

SearchContext::FINDERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SearchContext

#find_element, #find_elements

Constructor Details

#initialize(bridge, id) ⇒ Element

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new Element



14
15
16
# File 'lib/selenium/webdriver/common/element.rb', line 14

def initialize(bridge, id)
  @bridge, @id = bridge, id
end

Instance Attribute Details

#bridgeObject (readonly)

Returns the value of attribute bridge.



6
7
8
# File 'lib/selenium/webdriver/common/element.rb', line 6

def bridge
  @bridge
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



22
23
24
# File 'lib/selenium/webdriver/common/element.rb', line 22

def ==(other)
  other.kind_of?(self.class) && bridge.elementEquals(self, other)
end

#as_json(opts = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

For Rails 3 - jonathanjulian.com/2010/04/rails-to_json-or-as_json/



324
325
326
# File 'lib/selenium/webdriver/common/element.rb', line 324

def as_json(opts = nil)
  { :ELEMENT => @id }
end

#attribute(name) ⇒ String? Also known as: []

Get the value of a the given attribute of the element. Will return the current value, even if this has been modified after the page has been loaded. More exactly, this method will return the value of the given attribute, unless that attribute is not present, in which case the value of the property with the same name is returned. If neither value is set, nil is returned. The “style” attribute is converted as best can be to a text representation with a trailing semi-colon. The following are deemed to be “boolean” attributes, and will return either “true” or “false”:

async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked, defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate, iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate, nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking, selected, spellcheck, truespeed, willvalidate

Finally, the following commonly mis-capitalized attribute/property names are evaluated as expected:

class, readonly

Parameters:

Returns:

  • (String, nil)

    attribute value



86
87
88
# File 'lib/selenium/webdriver/common/element.rb', line 86

def attribute(name)
  bridge.getElementAttribute @id, name
end

#clearObject

Clear this element



137
138
139
# File 'lib/selenium/webdriver/common/element.rb', line 137

def clear
  bridge.clearElement @id
end

#clickObject

Click the element



35
36
37
# File 'lib/selenium/webdriver/common/element.rb', line 35

def click
  bridge.clickElement @id
end

#displayed?Boolean

Is the element displayed?

Returns:

  • (Boolean)


167
168
169
# File 'lib/selenium/webdriver/common/element.rb', line 167

def displayed?
  bridge.isElementDisplayed @id
end

#drag_and_drop_by(right_by, down_by) ⇒ Object

Drag and drop this element

Parameters:

  • right_by (Integer)

    number of pixels to drag right

  • down_by (Integer)

    number of pixels to drag down



257
258
259
260
# File 'lib/selenium/webdriver/common/element.rb', line 257

def drag_and_drop_by(right_by, down_by)
  warn "#{self.class}#drag_and_drop_{by,on} is deprecated. Please use Selenium::WebDriver::Driver#action (and Selenium::WebDriver::ActionBuilder) instead."
  bridge.dragElement @id, right_by, down_by
end

#drag_and_drop_on(other) ⇒ Object

Drag and drop this element on the given element

Parameters:



268
269
270
271
272
273
274
275
276
# File 'lib/selenium/webdriver/common/element.rb', line 268

def drag_and_drop_on(other)
  current_location = location()
  destination      = other.location

  right = destination.x - current_location.x
  down  = destination.y - current_location.y

  drag_and_drop_by right, down
end

#enabled?Boolean

Is the element enabled?

Returns:

  • (Boolean)


147
148
149
# File 'lib/selenium/webdriver/common/element.rb', line 147

def enabled?
  bridge.isElementEnabled @id
end

#hashObject



27
28
29
# File 'lib/selenium/webdriver/common/element.rb', line 27

def hash
  @id.hash ^ @bridge.hash
end

#inspectObject



18
19
20
# File 'lib/selenium/webdriver/common/element.rb', line 18

def inspect
  '#<%s:0x%x id=%s tag_name=%s>' % [self.class, hash*2, @id.inspect, tag_name.inspect]
end

#locationWebDriver::Point

Get the location of this element.

Returns:



224
225
226
# File 'lib/selenium/webdriver/common/element.rb', line 224

def location
  bridge.getElementLocation @id
end

#location_once_scrolled_into_viewWebDriver::Point

Determine an element’s location on the screen once it has been scrolled into view.

Returns:



234
235
236
# File 'lib/selenium/webdriver/common/element.rb', line 234

def location_once_scrolled_into_view
  bridge.getElementLocationOnceScrolledIntoView @id
end

#refObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

for SearchContext and execute_script



303
304
305
# File 'lib/selenium/webdriver/common/element.rb', line 303

def ref
  @id
end

#selectObject

Select this element



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/selenium/webdriver/common/element.rb', line 175

def select
  warn "#{self.class}#select is deprecated. Please use #{self.class}#click and determine the current state with #{self.class}#selected?"

  unless displayed?
    raise Error::ElementNotDisplayedError, "you may not select an element that is not displayed"
  end

  unless enabled?
    raise Error::InvalidElementStateError, "cannot select a disabled element"
  end

  unless selectable?
    raise Error::InvalidElementStateError, "you may only select options, radios or checkboxes"
  end

  click unless selected?
end

#selected?Boolean

Is the element selected?

Returns:

  • (Boolean)


157
158
159
# File 'lib/selenium/webdriver/common/element.rb', line 157

def selected?
  bridge.isElementSelected @id
end

#send_keys(*args) ⇒ Object Also known as: send_key

Send keystrokes to this element

Examples:

element.send_keys "foo"                     #=> value: 'foo'
element.send_keys "tet", :arrow_left, "s"   #=> value: 'test'
element.send_keys [:control, 'a'], :space   #=> value: ' '

Parameters:

See Also:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/selenium/webdriver/common/element.rb', line 114

def send_keys(*args)
  values = args.map do |arg|
    case arg
    when Symbol
      Keys[arg]
    when Array
      arg = arg.map { |e| e.kind_of?(Symbol) ? Keys[e] : e }.join
      arg << Keys[:null]

      arg
    else
      arg.to_s
    end
  end

  bridge.sendKeysToElement @id, values
end

#sizeWebDriver::Dimension

Get the size of this element



244
245
246
# File 'lib/selenium/webdriver/common/element.rb', line 244

def size
  bridge.getElementSize @id
end

#style(prop) ⇒ Object

Get the value of the given CSS property



214
215
216
# File 'lib/selenium/webdriver/common/element.rb', line 214

def style(prop)
  bridge.getElementValueOfCssProperty @id, prop
end

#submitObject

Submit this element



197
198
199
# File 'lib/selenium/webdriver/common/element.rb', line 197

def submit
  bridge.submitElement @id
end

#tag_nameString

Get the tag name of this element

Returns:



45
46
47
# File 'lib/selenium/webdriver/common/element.rb', line 45

def tag_name
  bridge.getElementTagName @id
end

#textString

Get the text content of this element

Returns:



96
97
98
# File 'lib/selenium/webdriver/common/element.rb', line 96

def text
  bridge.getElementText @id
end

#to_json(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert to a WebElement JSON Object for transmission over the wire.



314
315
316
# File 'lib/selenium/webdriver/common/element.rb', line 314

def to_json(*args)
  as_json.to_json(*args)
end

#toggleObject

Toggle this element



205
206
207
208
# File 'lib/selenium/webdriver/common/element.rb', line 205

def toggle
  warn "#{self.class}#toggle is deprecated. Please use #{self.class}#click and determine the current state with #{self.class}#selected?"
  bridge.toggleElement @id
end

#valueString

Get the value of this element

Returns:



55
56
57
58
# File 'lib/selenium/webdriver/common/element.rb', line 55

def value
  warn "#{self.class}#value is deprecated, please use #{self.class}#attribute('value')"
  bridge.getElementValue @id
end