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".
···
On Aug 12, 2:03 pm, Li Chen <chen_...@yahoo.com> wrote:
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 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?
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.
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;
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/<div class="header">
Thanks,
Li
###############tag.txt####################### #I change id="header" to class="header"
<tag1>
<tag2>
<div class="header">ABC </div>
</tag2>
</tag1>
tag1.rb:6: undefined method `xpath' for nil:NilClass (NoMethodError)