views:

463

answers:

3

basically i want to select a node (div) in which it's children node's(h1,b,h3) contain specified text.

<html>
<div id="contents">
<p>
<h1> Child text 1</h1>
<b> Child text 2 </b>
...
</p>
<h3> Child text 3 </h3>
</div>

i am expecting, /html/div/ not /html/div/h1

i have this below, but unfortunately returns the children, instead of the xpath to the div.

expression = "//div[contains(text(), "Child text 1")]"
doc.xpath(expression)

i am expecting, /html/div/ not /html/div/h1

So is there a way to do this simply with xpath syntax?

A: 

Maybe you can find useful tips to your question in this xpath tutorial

Daniel Cukier
+2  A: 

You could append "/.." to anchor back to the parent. Not sure if there's a more robust method.

expression = "//div[contains(text(), "Child text 1")]/.."
meder
what does that mean by append "/.."
ajfjaj
it means to add, try the example I just added.
meder
wow that works ! not sure what it means by anchor back to parent ? does it find the immediate ancestor? what if i need to go back to the root that contains the div ? so the parent's parent. would i just do expression = "//html[contains(text(), "Child text 1")]/.."
ajfjaj
A: 

The following expression gives a node (div) in which any children nodes (not just h1,b,h3) contain specified text:

doc.xpath('//div[//*[contains(text(), "Child text 1")]]')

you can refine that and return the div with the id contents like in you example:

doc.xpath('//div[@id="contents" and //*[contains(text(), "Child text 1")]]')
andre-r