Method: Capybara::Node::Element#text

Defined in:
lib/capybara/node/element.rb

#text(type = nil, normalize_ws: false) ⇒ String

Retrieve the text of the element. If ignore_hidden_elements is true, which it is by default, then this will return only text which is visible. The exact semantics of this may differ between drivers, but generally any text within elements with display:none is ignored. This behaviour can be overridden by passing :all to this method.

Parameters:

  • type (:all, :visible) (defaults to: nil)

    Whether to return only visible or all text

Returns:

  • (String)

    The text of the element


58
59
60
61
62
# File 'lib/capybara/node/element.rb', line 58

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