westonplatter
westonplatter

Reputation: 1495

Why does this Nokogiri XPath have a null return?

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: [email protected]'
puts '=> ' + doc.xpath(@xpath).to_s

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

Upvotes: 2

Views: 2030

Answers (1)

Kamiel Wanrooij
Kamiel Wanrooij

Reputation: 12404

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.

Upvotes: 5

Related Questions