Why does this Nokogiri XPath have a null return?

2019-01-28 12:55发布

I'm XPath-ing through a web page with NOKOGIRI. I'm familiar with XPath, but I cannot figure out why my XPath fails to pick up the specific row. See the ruby code.

I used FireBug XML to validate my XPath, so I am 99% sure my XPath is correct.

require 'nokogiri'
require 'open-uri'

@searchURL = 'http://www.umn.edu/lookup?UID=smit4562'
@xpath = '//html/body/p/table/tbody/tr/td[2]/table/tbody/tr[2]'

doc = Nokogiri::HTML(open(@searchURL))

puts 'row should be = Email Address: smit4562@umn.edu'
puts '=> ' + doc.xpath(@xpath).to_s

puts 'is row emppty?'
puts '=> ' + doc.xpath(@xpath).empty?().to_s

1条回答
戒情不戒烟
2楼-- · 2019-01-28 13:38

The <tbody> tag is an optional tag which is implicit if it is omitted. This means the <tbody> tags are inserted automatically by the browser when not present. They are not in the source code in your example, so nokogiri doesn't know about them.

Firebug uses the generated DOM, which does contains the tbody elements, so the statement does match inside a browser.

Remove both the tbody selectors and you should be fine.

查看更多
登录 后发表回答