2013-02-08 35 views
1

我试图匹配网站中的元描述,但无法取得过去的HTML实体。我试过逃脱角色和其他一些组合。如果您尝试在没有实体的情况下匹配文本的某个区域,则不存在任何问题。任何帮助apprecaited。Watir/Ruby HTML实体

require "rubygems" 
require "watir-webdriver" 

include Watir 

b = Watir::Browser.new :chrome 

b.goto('http://dev01-new.firestonecompleteautocare.com/cf/oil-change/motor-oil-and-your-car') 

if b.metas.any?{ |s| s.html.include? "Ever wonder what color your engine oil should 
be, or what affect temperature may have on it? Learn the answers to these 
frequently asked questions & more, here." } 
    puts 'Yes' 
else 
    puts 'No' 
end 

b.close 
+1

不要做'''包含Watir''''。这不是必需的,只会污染物体空间。 – 2013-02-10 14:57:04

回答

5

它看起来像watir-webdriver允许您根据其内容属性来定位metas。

所以,你可以这样做:

b.meta(:content => 'Ever wonder what color your engine oil should be, or what affect temperature may have on it? Learn the answers to these frequently asked questions & more, here.').exists? 
#=> true 

注意,在这里你可以使用而不必担心在HTML编码的实际字符。

+0

一如既往,谢谢Justin! – 2013-02-08 15:36:22

+0

只是为了说明为什么你的原始解决方案不起作用,这是因为watir-webdriver检索到的html有“?”为“?” (即,不编码)和“&”为“&”(即不同的编码)。不知道为什么它与查看源代码不同,但在匹配html时需要注意一些事项。 – 2013-02-08 15:43:17