Marco Dinatsoli
Marco Dinatsoli

Reputation: 10590

XPath how to get parents sibling inside node

This is my HTML:

<tr>
         <td bgcolor="ffffff" height="14" width="112"><p class="boldblack">&nbsp;Price:</p></td>
         <td bgcolor="ffffff" width="296"><p class="cena2">9 000 $</p></td>
         <td bgcolor="ffffff"></td>
</tr>

I want to take the 9 000

What I have tried

.//p[contains(., 'Price:')]

which gives me the Price: node. Now, how can I reach the 9000 from the Price node?

Note

I can't use XPath like td[2] because I am having a dynamic content. I just know that I will have the price node and their parent's brother will have the 9000 $

Update 1

I can't rely on class either because the structure of the HTML is very bad.

Upvotes: 1

Views: 224

Answers (1)

alecxe
alecxe

Reputation: 474221

One option would be to simply to rely on the class name (cena from Russian is price):

//p[@class="cena2"]/text()

If you want to rely on the preceding Price: label:

//tr[td[1]/p[contains(., "Price:")]]/td[2]/p/text()

Another option would be to check whether the text ends with $ sign:

//tr/td/p[ends-with(., "$")]/text()

As you see, there are multiple options, it is hard to tell which one is more reliable since you haven't showed the complete HTML code. You can even combine all 3 options I've presented.

Upvotes: 2

Related Questions