如何使用 Capybara 获取元素中的 HTML?
我正在编写一个黄瓜测试,我想在元素中获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以调用 HTML DOM
innerHTML
属性:应该适用于任何浏览器或驱动程序。
您可以在 w3schools 上查看所有可用属性
You can call HTML DOM
innerHTML
Property:Should work for any browser or driver.
You can check all available properties on w3schools
这篇文章很旧,但我想如果你仍然需要这个,我找到了一种方法。
要从 Capybara 元素访问 Nokogiri 节点(使用 Capybara 1.0.0beta1、Nokogiri 1.4.4),请尝试以下操作:
最后一部分可能会有所不同,但您应该能够在该节点变量中的某处找到 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:
The last part may vary for you, but you should be able to find the HTML somewhere in that node variable
在我的环境中, find 返回一个 Capybara::Element - 响应上面提到的 Eric Hu 的 :native 方法,它返回一个 Selenium::WebDriver::Element (对我来说)。然后 :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:
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.
看起来您可以执行
(node).native.inner_html
来获取 HTML 内容,例如使用这样的 HTML:您可以执行以下操作:
Looks like you can do
(node).native.inner_html
to get the HTML content, for example with HTML like this:You could do the following:
我遇到了与 Cyril Duchon-Doris 相同的问题,并且按照 https://github.com/teampoltergeist /poltergeist/issues/629 访问 Capybara::Poltergeist::Node 的 HTML 的方式是通过 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.:
大多数其他答案仅在 Racktest 中有效(因为它们使用 Racktest 特定的功能)。
如果您的驱动程序支持 javascript 评估(例如 Selenium),您可以使用
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
:如果您使用的是 Poltergeist 驱动程序,这些方法将允许您检查匹配的内容:
http://www.rubydoc.info/gems/poltergeist/1.5.1/Capybara/Poltergeist/Node
例如:
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:
尝试调用
find('//tr[2]//td[10]').node
来获取实际的 nokogiri 对象try calling
find('//tr[2]//td[10]').node
on it to get at the actual nokogiri object好吧,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.您还可以切换到 capybara-ui 并执行以下操作:
You could also switch to capybara-ui and do the following: