Jon Kruger
Jon Kruger

Reputation: 4069

How do I get the HTML in an element using Capybara?

I’m writing a cucumber test where I want to get the HTML in an element.

For example:

within 'table' do
  # this works
  find('//tr[2]//td[7]').text.should == "these are the comments" 

  # I want something like this (there is no "html" method)
  find('//tr[2]//td[7]').html.should == "these are the <b>comments</b>" 
end

Anyone know how to do this?

Upvotes: 86

Views: 57757

Answers (10)

llighterr
llighterr

Reputation: 982

You can call HTML DOM innerHTML Property:

find('//tr[2]//td[7]')['innerHTML']

Should work for any browser or driver. You can check all available properties on w3schools

Upvotes: 78

Geoff The
Geoff The

Reputation: 121

I ran into the same issue as Cyril Duchon-Doris, and per https://github.com/teampoltergeist/poltergeist/issues/629 the way to access the HTML of an Capybara::Poltergeist::Node is via the outerHTML property, e.g.:

find('//tr[2]//td[7]')['outerHTML']

Upvotes: 11

steel
steel

Reputation: 12520

You could also switch to capybara-ui and do the following:

# define your widget, in this case in your role
class User < Capybara::UI::Role
  widget :seventh_cell, [:xpath, '//tr[2]//td[7]']
end

# then in your tests
role = User.new

expect(role.widget(:seventh_cell).html).to eq(<h1>My html</h1>)

Upvotes: 0

Rimian
Rimian

Reputation: 38418

If you're using the Poltergeist driver, these methods will allow you to inspect what matches:

http://www.rubydoc.info/gems/poltergeist/1.5.1/Capybara/Poltergeist/Node

For example:

page.find('[name="form-field"]').native.value == 'something'

Upvotes: 2

Steve
Steve

Reputation: 886

In my environment, find returns a Capybara::Element - that responds to the :native method as Eric Hu mentioned above, which returns a Selenium::WebDriver::Element (for me). Then :text gets the contents, so it could be as simple as:

results = find(:xpath, "//td[@id='#{cell_id}']")
contents = results.native.text

if you're looking for the contents of a table cell. There's no content, inner_html, inner_text, or node methods on a Capybara::Element. Assuming people aren't just making things up, perhaps you get something different back from find depending on what else you have loaded with Capybara.

Upvotes: 20

Eric Hu
Eric Hu

Reputation: 18208

This post is old, but I think I found a way if you still need this.

To access the Nokogiri node from the Capybara element (using Capybara 1.0.0beta1, Nokogiri 1.4.4) try this:

elem = find('//tr[2]//td[10]')
node = elem.native
#This will give you a Nokogiri XML element

node.children[1].attributes["href"].value.should == "these are the &lt;b&gt;comments&lt;/b&gt;"

The last part may vary for you, but you should be able to find the HTML somewhere in that node variable

Upvotes: 27

Tobias Cohen
Tobias Cohen

Reputation: 20000

Looks like you can do (node).native.inner_html to get the HTML content, for example with HTML like this:

<div><strong>Some</strong> HTML</div>

You could do the following:

find('div').native.inner_html
=> '<strong>Some</strong> HTML'

Upvotes: 15

Andrei Botalov
Andrei Botalov

Reputation: 21096

Most of the other answers work only in Racktest (as they use Racktest-specific features).

If your driver supports javascript evaluation (like Selenium) you can use innerHTML :

html = page.evaluate_script("document.getElementById('my_id').innerHTML")

Upvotes: 4

Justin Halsall
Justin Halsall

Reputation: 19

try calling find('//tr[2]//td[10]').node on it to get at the actual nokogiri object

Upvotes: 1

Steve Ross
Steve Ross

Reputation: 4144

Well, Capybara uses Nokogiri to parse, so this page might be appropriate:

http://nokogiri.org/Nokogiri/XML/Node.html

I believe content is the method you are looking for.

Upvotes: 0

Related Questions