class Capybara::Node::Element
@see Capybara::Node
bar[:title]
bar.text
bar.value
element:
{Capybara::Node::Element} also has access to HTML attributes and other properties of the
bar.select(‘Baz’, from: ‘Quox’) # from Capybara::Node::Actions
bar = session.find(‘#bar’) # from Capybara::Node::Finders
session = Capybara::Session.new(:rack_test, my_app)
to interact with the contents of this element the same as with a document:
A {Capybara::Node::Element} represents a single element on the page. It is possible
#
def [](attribute)
-
(String)- The value of the attribute
Parameters:
-
attribute(Symbol) -- The attribute to retrieve
def [](attribute) synchronize { base[attribute] } end
def allow_reload!
def allow_reload! @allow_reload = true end
def checked?
-
(Boolean)- Whether the element is checked
def checked? synchronize { base.checked? } end
def click(*keys, **offset)
-
(Capybara::Node::Element)- The element
Options Hash:
(**offset)-
y(Integer) -- Y coordinate to offset the click location from the top left corner of the element -
x(Integer) -- X coordinate to offset the click location from the top left corner of the element
Parameters:
-
*modifier_keys(:alt, :control, :meta, :shift) -- ([]) Keys to be held down when clicking
Overloads:
-
click(*modifier_keys, **offset)
def click(*keys, **offset) synchronize { base.click(keys, offset) } self end
def disabled?
-
(Boolean)- Whether the element is disabled
def disabled? synchronize { base.disabled? } end
def double_click(*keys, **offset)
-
(Capybara::Node::Element)- The element
def double_click(*keys, **offset) synchronize { base.double_click(keys, offset) } self end
def drag_to(node)
-
(Capybara::Node::Element)- The element
Parameters:
-
node(Capybara::Node::Element) -- The element to drag to
def drag_to(node) synchronize { base.drag_to(node.base) } self end
def evaluate_async_script(script, *args)
-
(Object)- The result of the evaluated JavaScript (may be driver specific)
Parameters:
-
script(String) -- A string of JavaScript to evaluate
def evaluate_async_script(script, *args) session.evaluate_async_script(<<~JS, self, *args) (function (){ #{script} }).apply(arguments[0], Array.prototype.slice.call(arguments,1)); JS end
def evaluate_script(script, *args)
-
(Object)- The result of the evaluated JavaScript (may be driver specific)
Parameters:
-
script(String) -- A string of JavaScript to evaluate
def evaluate_script(script, *args) session.evaluate_script(<<~JS, self, *args) (function(){ return #{script.strip} }).apply(arguments[0], Array.prototype.slice.call(arguments,1)); JS end
def execute_script(script, *args)
-
args() -- Optional arguments that will be passed to the script. Driver support for this is optional and types of objects supported may differ between drivers -
script(String) -- A string of JavaScript to execute
def execute_script(script, *args) session.execute_script(<<~JS, self, *args) (function (){ #{script} }).apply(arguments[0], Array.prototype.slice.call(arguments,1)); JS end
def hover
-
(Capybara::Node::Element)- The element
def hover synchronize { base.hover } self end
def initialize(session, base, query_scope, query)
def initialize(session, base, query_scope, query) super(session, base) @query_scope = query_scope @query = query @allow_reload = false end
def inspect
def inspect %(#<Capybara::Node::Element tag="#{base.tag_name}" path="#{base.path}">) rescue NotSupportedByDriverError %(#<Capybara::Node::Element tag="#{base.tag_name}">) rescue StandardError => err raise unless session.driver.invalid_element_errors.any? { |et| err.is_a?(et) } %(Obsolete #<Capybara::Node::Element>) end
def multiple?
-
(Boolean)- Whether the element supports multiple results.
def multiple? synchronize { base.multiple? } end
def native
-
(Object)- The native element from the driver, this allows access to driver specific methods
def native synchronize { base.native } end
def path
-
(String)- An XPath expression
def path synchronize { base.path } end
def readonly?
-
(Boolean)- Whether the element is readonly
def readonly? synchronize { base.readonly? } end
def reload
def reload if @allow_reload begin reloaded = query_scope.reload.first(@query.name, @query.locator, @query.options) @base = reloaded.base if reloaded rescue StandardError => err raise err unless catch_error?(err) end end self end
def right_click(*keys, **offset)
-
(Capybara::Node::Element)- The element
def right_click(*keys, **offset) synchronize { base.right_click(keys, offset) } self end
def select_option
-
(Capybara::Node::Element)- The element
def select_option warn "Attempt to select disabled option: #{value || text}" if disabled? synchronize { base.select_option } self end
def selected?
-
(Boolean)- Whether the element is selected
def selected? synchronize { base.selected? } end
def send_keys(*args)
-
(Capybara::Node::Element)- The element
Parameters:
-
keys(String, Symbol, Array) --
Overloads:
-
send_keys(keys, ...)
def send_keys(*args) synchronize { base.send_keys(*args) } self end
def set(value, **options)
-
(Capybara::Node::Element)- The element
Parameters:
-
options(Hash{}) -- Driver specific options for how to set the value. Take default values from `Capybara#default_set_options` - See {Capybara::configure} -
value(String) -- The new value
def set(value, **options) raise Capybara::ReadOnlyElementError, "Attempt to set readonly element with value: #{value}" if readonly? options = session_options.default_set_options.to_h.merge(options) synchronize { base.set(value, options) } self end
def style(*styles)
-
(Hash)- Hash of the CSS property names to computed values
Parameters:
-
styles(Array) -- Names of the desired CSS properties
def style(*styles) styles = styles.flatten.map(&:to_s) raise ArgumentError, 'You must specify at least one CSS style' if styles.empty? begin synchronize { base.style(styles) } rescue NotImplementedError => err begin evaluate_script(STYLE_SCRIPT, *styles) rescue Capybara::NotSupportedByDriverError raise err end end end
def tag_name
-
(String)- The tag name of the element
def tag_name synchronize { base.tag_name } end
def text(type = nil, normalize_ws: false)
-
(String)- The text of the element
Parameters:
-
type(:all, :visible) -- Whether to return only visible or all text
def text(type = nil, normalize_ws: false) type ||= :all unless session_options.ignore_hidden_elements || session_options.visible_text_only txt = synchronize { type == :all ? base.all_text : base.visible_text } normalize_ws ? txt.gsub(/[[:space:]]+/, ' ').strip : txt end
def trigger(event)
-
(Capybara::Node::Element)- The element
Parameters:
-
event(String) -- The name of the event to trigger
def trigger(event) synchronize { base.trigger(event) } self end
def unselect_option
-
(Capybara::Node::Element)- The element
def unselect_option synchronize { base.unselect_option } self end
def value
-
(String)- The value of the form element
def value synchronize { base.value } end
def visible?
-
(Boolean)- Whether the element is visible
def visible? synchronize { base.visible? } end