如何使用 Capybara 获取元素中的 HTML?

发布于 2024-09-29 19:00:59 字数 393 浏览 3 评论 0原文

我正在编写一个黄瓜测试,我想在元素中获取 HTML。

例如:

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

有人知道该怎么做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

一百个冬季 2024-10-06 19:00:59

您可以调用 HTML DOM innerHTML 属性:

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

应该适用于任何浏览器或驱动程序。
您可以在 w3schools 上查看所有可用属性

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

泛泛之交 2024-10-06 19:00:59

这篇文章很旧,但我想如果你仍然需要这个,我找到了一种方法。

要从 Capybara 元素访问 Nokogiri 节点(使用 Capybara 1.0.0beta1、Nokogiri 1.4.4),请尝试以下操作:

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

最后一部分可能会有所不同,但您应该能够在该节点变量中的某处找到 HTML

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

〗斷ホ乔殘χμё〖 2024-10-06 19:00:59

在我的环境中, find 返回一个 Capybara::Element - 响应上面提到的 Eric Hu 的 :native 方法,它返回一个 Selenium::WebDriver::Element (对我来说)。然后 :text 获取内容,因此它可能很简单:

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

如果您正在查找表格单元格的内容。 Capybara::Element 上没有内容、inner_html、inner_text 或节点方法。假设人们不只是编造事情,也许你会从 find 中得到不同的东西,这取决于你在水豚中加载的其他内容。

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.

困倦 2024-10-06 19:00:59

看起来您可以执行 (node).native.inner_html 来获取 HTML 内容,例如使用这样的 HTML:

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

您可以执行以下操作:

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

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'
养猫人 2024-10-06 19:00:59

我遇到了与 Cyril Duchon-Doris 相同的问题,并且按照 https://github.com/teampoltergeist /poltergeist/issues/629 访问 Capybara::Poltergeist::Node 的 HTML 的方式是通过 outerHTML 属性,例如:

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

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']
守护在此方 2024-10-06 19:00:59

大多数其他答案仅在 Racktest 中有效(因为它们使用 Racktest 特定的功能)。

如果您的驱动程序支持 javascript 评估(例如 Selenium),您可以使用 innerHTML

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

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")
清音悠歌 2024-10-06 19:00:59

如果您使用的是 Poltergeist 驱动程序,这些方法将允许您检查匹配的内容:

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

例如:

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

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'
姐不稀罕 2024-10-06 19:00:59

尝试调用 find('//tr[2]//td[10]').node 来获取实际的 nokogiri 对象

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

掐死时间 2024-10-06 19:00:59

好吧,Capybara 使用 Nokogiri 进行解析,因此此页面可能合适:

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

我相信 content 就是您正在寻找的方法。

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.

漫雪独思 2024-10-06 19:00:59

您还可以切换到 capybara-ui 并执行以下操作:

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

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>)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文