class Element
An element represents a DOM element. This class is used to interact with the DOM.
element = session.find_element(:css, "main#content")
element.click
Nested
Definitions
def initialize(session, id)
Initialize the element.
Signature
-
parameter
session
Session
The session the element belongs to.
-
parameter
id
String
The element identifier.
Implementation
def initialize(session, id)
@session = session
@delegate = session.delegate
@id = id
@attributes = nil
@properties = nil
end
def as_json
Signature
-
returns
Hash
The JSON representation of the element.
Implementation
def as_json
{ELEMENT_KEY => @id}
end
def to_json(...)
Signature
-
returns
String
The JSON representation of the element.
Implementation
def to_json(...)
as_json.to_json(...)
end
attr :session
Signature
-
attribute
Session
The session the element belongs to.
attr :delegate
Signature
-
attribute
Protocol::HTTP::Middleware
The underlying HTTP client (or wrapper).
attr :id
Signature
-
attribute
String
The element identifier.
def request_path(path = nil)
The path used for making requests to the web driver bridge.
Signature
-
parameter
path
String | Nil
The path to append to the request path.
-
returns
String
The path used for making requests to the web driver bridge.
Implementation
def request_path(path = nil)
if path
"/session/#{@session.id}/element/#{@id}/#{path}"
else
"/session/#{@session}/element/#{@id}"
end
end
def current_scope
The current scope to use for making subsequent requests.
Signature
-
returns
Element
The element.
Implementation
def current_scope
self
end
def execute(script, *arguments)
Execute a script in the context of the element. this
will be the element.
Signature
-
parameter
script
String
The script to execute.
-
parameter
arguments
Array
The arguments to pass to the script.
Implementation
def execute(script, *arguments)
@session.execute("return (function(){#{script}}).call(...arguments)", self, *arguments)
end
def execute_async(script, *arguments)
Execute a script in the context of the element. this
will be the element.
Signature
-
parameter
script
String
The script to execute.
-
parameter
arguments
Array
The arguments to pass to the script.
Implementation
def execute_async(script, *arguments)
@session.execute_async("return (function(){#{script}}).call(...arguments)", self, *arguments)
end
def attribute(name)
Get the value of an attribute.
Given an attribute name, e.g. href
, this method will return the value of the attribute, as if you had executed the following JavaScript:
element.getAttribute("href")
Signature
-
parameter
name
String
The name of the attribute.
-
returns
Object
The value of the attribute.
Implementation
def attribute(name)
get("attribute/#{name}")
end
def set_attribute(name, value)
Set the value of an attribute.
Signature
-
parameter
name
String
The name of the attribute.
-
parameter
value
Object
The value of the attribute.
Implementation
def set_attribute(name, value)
execute("this.setAttribute(...arguments)", name, value)
end
def attributes
Get attributes associated with the element.
Signature
-
returns
Attributes
The attributes associated with the element.
Implementation
def attributes
@attributes ||= Attributes.new(self)
end
def property(name)
Get the value of a property.
Given a property name, e.g. offsetWidth
, this method will return the value of the property, as if you had executed the following JavaScript:
element.offsetWidth
Signature
-
parameter
name
String
The name of the property.
-
returns
Object
The value of the property.
Implementation
def property(name)
get("property/#{name}")
end
def css(name)
Get the value of a CSS property.
Given a CSS property name, e.g. width
, this method will return the value of the property, as if you had executed the following JavaScript:
window.getComputedStyle(element).width
Signature
-
parameter
name
String
The name of the CSS property.
-
returns
String
The value of the CSS property.
Implementation
def css(name)
get("css/#{name}")
end
def text
Get the text content of the element.
This method will return the text content of the element, as if you had executed the following JavaScript:
element.textContent
Signature
-
returns
String
The text content of the element.
Implementation
def text
get("text")
end
def tag_name
Get the element's tag name.
This method will return the tag name of the element, as if you had executed the following JavaScript:
element.tagName
Signature
-
returns
String
The tag name of the element.
Implementation
def tag_name
get("name")
end
Rectangle = Struct.new(:x, :y, :width, :height)
A struct representing the size of an element.
def rectangle
Get the element's bounding rectangle.
Signature
-
returns
Rectangle
The element's bounding rectangle.
Implementation
def rectangle
get("rect").tap do |reply|
Rectangle.new(reply["x"], reply["y"], reply["width"], reply["height"])
end
end
def selected?
Whether the element is selected OR checked.
Signature
-
returns
Boolean
True if the element is selected OR checked.
Implementation
def selected?
get("selected")
end
def enabled?
Whether the element is enabled.
Signature
-
returns
Boolean
True if the element is enabled.
Implementation
def enabled?
get("enabled")
end
def displayed?
Whether the element is displayed.
Signature
-
returns
Boolean
True if the element is displayed.
Implementation
def displayed?
get("displayed")
end
def click
Click the element.
Implementation
def click
post("click")
end
def clear
Clear the element.
Implementation
def clear
post("clear")
end
def send_keys(text)
Send keys to the element. Simulates a user typing keys while the element is focused.
Implementation
def send_keys(text)
post("value", {text: text})
end
def frame?
Whether the element is a frame.
Implementation
def frame?
FRAME_TAGS.include?(self.tag_name)
end