0

I have the following HTML snippet:

   <divv id="items">
        <trr>
            <td>
                <p>Cars</p>
                <a>Toyota</a>
                <a>Opel</a>
                <a>Audi</a>
            </td>
        </tr>
        <tr>
            <td>
                <p>Planes</p>
                <a>A320</a>
                <a>B787</a>
                <a>B767</a>
            </td>
        </tr>
    <div/>

What I want is to create a XPath query so I can retrieve only the Cars.
Currently I am using this: //div[@id='items']/tr/td. But with this I get also the Plane items. I do not know how to test for the 'p' tag.

Anyone can help me ?
Thanks.

Adrian
  • 19,440
  • 34
  • 112
  • 219
  • I don't think that you can, since the cars *are not contained within the `p` element* unless you just take the first group (and assume that they are the cars). – lnafziger Dec 16 '12 at 20:55
  • What is the syntax for taking the first group ? I am not really good at this xpath queries – Adrian Dec 16 '12 at 20:59

2 Answers2

2
//div[@id='items']/tr/td[p='Cars']

The last predicate tests the existence of a <p> child element with Cars text content and thus filters out the <td> with <p>Planes</p>.

jasso
  • 13,736
  • 2
  • 36
  • 50
1

If picking the first group is enough, then you can use:

//div[@id='items']/tr/td[1]
lnafziger
  • 25,760
  • 8
  • 60
  • 101