Capybara没有找到元标签

Capybara 2.1.0似乎没有找到任何元标记:

(rdb:1) p page.find 'meta' *** Capybara::ElementNotFound Exception: Unable to find css "meta" 

即使它们出现在page.source

 (rdb:1) p page.source "\n\n\nMyTitle\n\n\n\n\n\n..." 

解决方案是在findhave_selector使用:visible => false

 page.should have_css 'meta[name="description"]', :visible => false 

要么:

 page.find 'meta[name="description"]', :visible => false 

如果要检查更具体的元数据,包括描述文本等:

https://gist.github.com/Lordnibbler/6247531

 RSpec::Matchers.define :have_meta do |name, expected| match do |actual| has_css?("meta[name='#{name}'][content='#{expected}']") end failure_message_for_should do |actual| actual = first("meta[name='#{name}']") if actual "expected that meta #{name} would have content='#{expected}' but was '#{actual[:content]}'" else "expected that meta #{name} would exist with content='#{expected}'" end end end RSpec::Matchers.define :have_title do |expected| match do |actual| has_css?("title", :text => expected) end failure_message_for_should do |actual| actual = first("title") if actual "expected that title would have been '#{expected}' but was '#{actual.text}'" else "expected that title would exist with '#{expected}'" end end end 

然后,像这样查找元:

page.should have_meta(:description, 'Brand new Ruby on Rails TShirts')

借用施普雷的爱: https : //github.com/spree/spree/blob/master/core/lib/spree/testing_support/capybara_ext.rb