【问题标题】:Watir / Ruby HTML EntitiesWatir / Ruby HTML 实体
【发布时间】:2013-02-08 14:37:55
【问题描述】:

我正在尝试匹配网站中的元描述,但无法获取过去的 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

【问题讨论】:

  • 不要做include Watir。它不是必需的,只会污染对象空间。

标签: ruby watir watir-webdriver


【解决方案1】:

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

所以你可以这样做:

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 编码。

【讨论】:

  • 只是要注意为什么您的原始解决方案不起作用,这是因为 watir-webdriver 检索到的 html 有“?”作为 ”?” (即没有编码)和“&”作为“&” (即不同的编码)。不知道为什么它与查看源代码不同,但在匹配 html 时要记住一些事情。
猜你喜欢
  • 2011-08-08
  • 2020-05-25
  • 2016-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多