# Hpricot: match inner\_text then return parent

**URL:** <https://www.ruby-forum.com/t/hpricot-match-inner-text-then-return-parent/145163>\
**Category:** Ruby\
**Created:** [August 6, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/hpricot-match-inner-text-then-return-parent/145163 "2008-08-06T16:28:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [August 6, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/hpricot-match-inner-text-then-return-parent/145163/1 "2008-08-06T16:28:17Z")

</div>

Hello,

Here is sample HTML:

| abcd | efgh | ijkl |
|
| |
| mnop | qrst | uvwx |

I have a requirement that I need to match for example the inner\_text  
“qrst”. If I find it, then I want to grab the immediate parent. So for  
example, I want qrst, if a a.inner\_text == qrst then grab parent and the  
result I want is:

 mnop qrstuvwx

My very wrong attempt:

```
  (doc/"html//body//form//table//tbody//tr//td").each do |row|

      result = row.search("a").select {|ele|
        if ele.inner_text.to_s == mac_addr
          puts "I'm in"
          parent = ele.nodes_at(-1)
          puts parent
        end
      }

  end

```

I can’t seem to grab just one selection let alone grab the parent ☹

Thank you

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [August 6, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/hpricot-match-inner-text-then-return-parent/145163/2 "2008-08-06T20:36:19Z")

</div>

Thanks for Ryan52 on IRC. It ended up being simpler than I thought:

For input file:

| abcd | efgh | ijkl |
|
| |
| mnop | qrst | uvwx |

The following with parse and then find the element and then return the  
parent:

require ‘rubygems’  
require ‘hpricot’

```
  doc = open("test.html") { |f| Hpricot(f)}

  result = 

```

(doc/“tr”).search(“a[text()\*=‘uvwx’]”).first.parent.parent  
puts result

ga rg wrote:

> Hello,
> 
> Here is sample HTML:
> 
> | abcd | efgh | ijkl |
> |
> | |
> | mnop | qrst | uvwx |
> 
> I have a requirement that I need to match for example the inner\_text  
> “qrst”. If I find it, then I want to grab the immediate parent. So for  
> example, I want qrst, if a a.inner\_text == qrst then grab parent and the  
> result I want is:
> 
> mnop qrstuvwx
> 
> My very wrong attempt:
> 
> ```
> (doc/"html//body//form//table//tbody//tr//td").each do |row|
> 
> result = row.search("a").select {|ele|
> if ele.inner_text.to_s == mac_addr
> puts "I'm in"
> parent = ele.nodes_at(-1)
> puts parent
> end
> }
> 
> end
> 
> ```
> 
> I can’t seem to grab just one selection let alone grab the parent ☹
> 
> Thank you
