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

Jon Kruger picture Jon Kruger · Nov 1, 2010 · Viewed 46.7k times · Source

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?

Answer

llighterr picture llighterr · May 18, 2014

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