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 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



31
32
33
# File 'lib/selenium/webdriver/common/element.rb', line 31

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

Instance Method Details

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



39
40
41
# File 'lib/selenium/webdriver/common/element.rb', line 39

def ==(other)
  other.kind_of?(self.class) && ref == other.ref
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/



293
294
295
296
297
298
# File 'lib/selenium/webdriver/common/element.rb', line 293

def as_json(opts = nil)
  {
    :ELEMENT => @id,
    "element-6066-11e4-a52e-4f735466cecf" => @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:

  • attribute (String)

    name

Returns:

  • (String, nil)

    attribute value



116
117
118
# File 'lib/selenium/webdriver/common/element.rb', line 116

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

#clearObject

If this element is a text entry element, this will clear the value. Has no effect on other elements. Text entry elements are INPUT and TEXTAREA elements.

Note that the events fired by this event may not be as you’d expect. In particular, we don’t fire any keyboard or mouse events. If you want to ensure keyboard events are fired, consider using #send_keys with the backspace key. To ensure you get a change event, consider following with a call to #send_keys with the tab key.



159
160
161
# File 'lib/selenium/webdriver/common/element.rb', line 159

def clear
  bridge.clearElement @id
end

#clickObject

Click this element. If this causes a new page to load, this method will attempt to block until the page has loaded. At this point, you should discard all references to this element and any further operations performed on this element will raise a StaleElementReferenceError unless you know that the element and the page will still be present. If click() causes a new page to be loaded via an event or is done by sending a native event then the method will not wait for it to be loaded and the caller should verify that a new page has been loaded.

There are some preconditions for an element to be clicked. The element must be visible and it must have a height and width greater then 0.

Equivalent to:

driver.action.click(element)

Examples:

Click on a button


driver.find_element(:tag_name, "button").click

Raises:

  • (StaleElementReferenceError)

    if the element no longer exists as defined



72
73
74
# File 'lib/selenium/webdriver/common/element.rb', line 72

def click
  bridge.clickElement @id
end

#css_value(prop) ⇒ Object Also known as: style

Get the value of the given CSS property

Note that shorthand CSS properties (e.g. background, font, border, border-top, margin, margin-top, padding, padding-top, list-style, outline, pause, cue) are not returned, in accordance with the DOM CSS2 specification - you should directly access the longhand properties (e.g. background-color) to access the desired values.



212
213
214
# File 'lib/selenium/webdriver/common/element.rb', line 212

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

#displayed?Boolean

Is the element displayed?

Returns:

  • (Boolean)


189
190
191
# File 'lib/selenium/webdriver/common/element.rb', line 189

def displayed?
  bridge.isElementDisplayed @id
end

#enabled?Boolean

Is the element enabled?

Returns:

  • (Boolean)


169
170
171
# File 'lib/selenium/webdriver/common/element.rb', line 169

def enabled?
  bridge.isElementEnabled @id
end

#hashObject



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

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

#inspectObject



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

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

#locationWebDriver::Point

Get the location of this element.

Returns:



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

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:



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

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



272
273
274
# File 'lib/selenium/webdriver/common/element.rb', line 272

def ref
  @id
end

#selected?Boolean

Is the element selected?

Returns:

  • (Boolean)


179
180
181
# File 'lib/selenium/webdriver/common/element.rb', line 179

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:

  • (String, Symbol, Array)

See Also:



144
145
146
# File 'lib/selenium/webdriver/common/element.rb', line 144

def send_keys(*args)
  bridge.sendKeysToElement @id, Keys.encode(args)
end

#sizeWebDriver::Dimension

Get the size of this element



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

def size
  bridge.getElementSize @id
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 the element.

Examples:

Get the tagname of an INPUT element(returns “input”)


driver.find_element(:xpath, "//input").tag_name

Returns:

  • (String)

    The tag name of this element.



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

def tag_name
  bridge.getElementTagName @id
end

#textString

Get the text content of this element

Returns:

  • (String)


126
127
128
# File 'lib/selenium/webdriver/common/element.rb', line 126

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.



283
284
285
# File 'lib/selenium/webdriver/common/element.rb', line 283

def to_json(*args)
  JSON.generate as_json
end