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

僤鯓⒐⒋嵵緔 提交于 2019-12-17 18:32:04

问题


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?


回答1:


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




回答2:


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 <b>comments</b>"

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




回答3:


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.




回答4:


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'



回答5:


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']



回答6:


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")



回答7:


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




回答8:


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.




回答9:


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'



回答10:


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>)


来源:https://stackoverflow.com/questions/4071937/how-do-i-get-the-html-in-an-element-using-capybara

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!