StackOverflowNewbie
StackOverflowNewbie

Reputation: 40633

XPath: get a node's sibling's string content

Assume the following HTML DOM:

<table id="my_id">
    <tbody>
        <tr>
            <th>Location:</th>
            <td>
                1600 Parkway Ave
                Los Angels
                California
            </td>
        </tr>
    </tbody>
</table>

How do I get '1600 Parkway Ave Los Angels California' assuming there are a lot of <tr> in this table? I think I need to get the sibling of the <th> that contains Location:. I've been trying to do something like:

//*[@id="my_id"]//th[Text()='Location:']

Upvotes: 0

Views: 40

Answers (2)

Daniel Haley
Daniel Haley

Reputation: 52848

You could move the th predicate up to tr...

//*[@id="my_id"]//tr[th='Location:']/td/text()

Update based on comment The "Location:" string actually has whitespace around it.:

//*[@id="my_id"]//tr[normalize-space(th)='Location:']/td/text()

Upvotes: 3

hek2mgl
hek2mgl

Reputation: 157937

As simple as:

//*[@id="my_id"]//th[text()='Location:']/../td/text()

Upvotes: 0

Related Questions