user001
user001

Reputation: 515

Get text from Capybara node element

I am trying get the text value of table header with a classname in Capybara. I getting the node element in Capybara but I am not able to get the text content of the class when I use the text attribute. How can I get the text of the Capybara node element here?

html.erb

<% @tests.each do |test| %>        
    <th class="test_name_header"><%= test.name %></th>
<% end %>

capybara_test.rb

all(:css, '.test_name_header', :visible => false).each do |el|
      puts el.text
end

This code does not print the text of the element. Nothing is printed but when I puts el in the above code it prints the node element.

Upvotes: 2

Views: 4599

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49950

The docs for #text - https://www.rubydoc.info/github/teamcapybara/capybara/Capybara/Node/Element#text-instance_method - state that by default it only gets visible text. Since you're specifying visible: false in your all call I am assuming these headers are not actually visible on the page. If that is the case then, as mentioned in the docs, you'd need to do

el.text(:all)

to get non-visible text.

Upvotes: 6

Related Questions