Hpricot and xpath

Hi all,

I want to find the path to a tag I am interested using hpricot.
According to the document and examples I think I should use #xpath
method.

I expect that ruby will return the path as this:
“/tag1/tag2/div[@id=‘header’]”

but I get the following info:

tag1.rb:4: undefined method `xpath’ for nil:NilClass (NoMethodError)

I wonder why method #xpath is not defined?

Thanks,

Li

###########################################
Here is my code:

require ‘hpricot’
doc=Hpricot(open(‘tag.txt’))
puts doc
puts doc.at(“header”).xpath()

########tag.txt##########




On Aug 12, 2:03 pm, Li Chen [email protected] wrote:

tag1.rb:4: undefined method `xpath’ for nil:NilClass (NoMethodError)

I wonder why method #xpath is not defined?

Look carefully. It’s not defined for nil. Which means that
doc.at(“header”) is returning nil. That’s because there are no
elements with the name of “header”; only an attribute. Try using the
css selector “#header”.

Mark T. wrote:

css selector “#header”.

Oops - .header would have been a class, and #header is for an id. I got
them
backwards!

Mark T. wrote:

Look carefully. It’s not defined for nil. Which means that
doc.at(“header”) is returning nil. That’s because there are no
elements with the name of “header”; only an attribute. Try using the
css selector “#header”.

Here is the result after I change to css selector or xpath;

puts doc.at("#header").xpath()

ag1.rb:4: undefined method `xpath’ for {elem

{text "
\n" " "}}:Hpricot::Elem (NoMethodError)

puts doc.at("#header").css_path()

tag1.rb:4: undefined method `css_path’ for {elem

{text
" \n" " "}}:Hpricot::Elem (NoMethodError)

Li

Are you using an old version of Hpricot? When I run this code:

#!/usr/bin/env ruby
require ‘rubygems’
require ‘hpricot’
doc = Hpricot(open(“tag.txt”))
puts doc.at("#header").xpath

I get the following result:
//div[@id=‘header’]

I’m using Hpricot 0.6 (do a gem list --local to see your version)

Li Chen wrote:

I wonder why method #xpath is not defined?

You gotta learn what those errors look like. If you have a ‘nil’ where
you don’t
expect it, the error happens when you use the nil incorrectly, such as
calling a
method - like xpath - that nil does not have.

Your doc.at(“header”) returns nil.

require ‘hpricot’
doc=Hpricot(open(‘tag.txt’))
puts doc
puts doc.at(“header”).xpath()

What does p doc.at(‘header’) say? Would a .search (or equivalent) for
‘.header’
work?

Mark T. wrote:

Are you using an old version of Hpricot? When I run this code:

#!/usr/bin/env ruby
require ‘rubygems’
require ‘hpricot’
doc = Hpricot(open(“tag.txt”))
puts doc.at("#header").xpath

I get the following result:
//div[@id=‘header’]

I’m using Hpricot 0.6 (do a gem list --local to see your version)

My version is 0.4. Since I can not install the new version remotely.
I want to install it locally. But I can’t find hpricot gem in Rubyfore.

Li

Li Chen wrote:

I’m using Hpricot 0.6 (do a gem list --local to see your version)

My version is 0.4. Since I can not install the new version remotely.
I want to install it locally. But I can’t find hpricot gem in Rubyfore.

Thank you all for the help.Now it works after install to the 0.6
version.

Li

tag1.rb:6: undefined method `xpath’ for nil:NilClass (NoMethodError)

Do you see the nil in that line? Where is the nil coming from? Is the
method
before the .xpath, on line 6, possibly returning a nil and not an Elem?

After pondering that, switch ‘#header’ to ‘.header’!

Hi all,

I find that in my code ‘xpath’ only works if the attribute is ‘id’. How
to explain it?

Another question: is it possible to get the path/tree relationship of a
tag(including its attributes)in the following format using hpricot or
the position of an interested tag within a html page/file:

tag1/tag2/

Thanks,

Li

###############tag.txt#######################
#I change id=“header” to class=“header”


ABC



tag1.rb:6: undefined method `xpath’ for nil:NilClass (NoMethodError)

Exit code: 1

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs