# Hpricot and path of an elememt

**URL:** <https://www.ruby-forum.com/t/hpricot-and-path-of-an-elememt/145496>\
**Category:** Ruby\
**Created:** [August 10, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/hpricot-and-path-of-an-elememt/145496 "2008-08-10T20:38:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [August 10, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/hpricot-and-path-of-an-elememt/145496/1 "2008-08-10T20:38:56Z")

</div>

Hi all,

I use hpricot to load a page. Then I try to find the path for an  
element “font”() in the page. Here is  
the tutorial  
([http://code.whytheluckystiff.net/hpricot/wiki/HpricotBasics](http://code.whytheluckystiff.net/hpricot/wiki/HpricotBasics)):

doc.at(“#header”).xpath  
#=\> “//div[@id=‘header’]”

here is my code:  
puts doc.at(“#font”).xpath

When I run the code Ruby complains undefined method for xpath. I wonder  
if I have problem understanding the tutorial.

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [August 10, 2008, 9:05pm UTC](https://www.ruby-forum.com/t/hpricot-and-path-of-an-elememt/145496/2 "2008-08-10T21:05:30Z")

</div>

On Sunday 10 August 2008 13:36:42 Li Chen wrote:

> I use hpricot to load a page. Then I try to find the path for an  
> element “font”() in the page.

So, you probably want:

(doc / ‘font’)

> doc.at("#header").xpath  
> #=\> “//div[@id=‘header’]”

Right, that’s searching for a tag that looks like this:

> here is my code:  
> puts doc.at("#font").xpath

And that’s searching for a tag that looks like this:

If you’re following that example, you probably want:

puts doc.at(‘font’).xpath

Now, first question: Why do you need the xpath? Usually, the idea is to  
try to  
find that element, and then do something with it. So, for example:

# To return all text:

(doc / ‘font’).text

# To loop over each font element:

(doc / ‘font’).each { |tag|  
puts tag.inner\_text  
}

Second question: Why is there a font tag on this page? If you had any  
hand in  
creating the page, shame on you – go learn some CSS.

In fact, go learn some CSS anyway. Hpricot supports both CSS selectors  
and  
XPath, and it’s usually much easier to use the selectors. Years later, I  
still remember, roughly, how selectors work – but only a few months  
later,  
I’ve almost completely forgotten XPath.

There are things XPath can do that selectors can’t. But until you  
encounter  
them, XPath is overkill.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [August 11, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/hpricot-and-path-of-an-elememt/145496/3 "2008-08-11T15:01:24Z")

</div>

David M. wrote:

> Now, first question: Why do you need the xpath? Usually, the idea is to  
> try to  
> find that element, and then do something with it. So, for example:

> # To return all text:
> 
> (doc / ‘font’).text
> 
> # To loop over each font element:
> 
> (doc / ‘font’).each { |tag|  
> puts tag.inner\_text  
> }

I need to extract text within this tag. I follow you code and I find

1. (doc/‘font’).text and (doc/‘font’).html return the same results
2. when I run (doc / ‘font’).each { |tag| puts tag.inner\_text}  
Ruby complains it:  
undefined method `inner\_text’ for #Hpricot::Elem:0x2e9f9c4  
(NoMethodError)

so I change it to tag.inner\_html and it works. I check the document  
about hpricot and find the methode #inner\_text is there. But I cannot  
figure out why Ruby complains about it.

> Second question: Why is there a font tag on this page? If you had any  
> hand in  
> creating the page, shame on you – go learn some CSS.

I am a newbie on HTML and website development. If you want to know why  
there is a font tag in the page, please check this out:  
[http://www.ensembl.org/Homo\_sapiens/exonview?db=core;transcript=ENST00000356766](http://www.ensembl.org/Homo_sapiens/exonview?db=core;transcript=ENST00000356766)

What I try to do is to extract some info I am interested from this  
page. I have no idea why they put this tag and that tag there. I don’t  
think it is my priority to know somany whys now. I am more concerned  
about letting the job done.

Anyway thank very much for the tips.

Li
