using xpath to select an element after another

jseabold picture jseabold · Oct 9, 2013 · Viewed 21.4k times · Source

I've seen similar questions, but the solutions I've seen won't work on the following. I'm far from an XPath expert. I just need to parse some HTML. How can I select the table that follows Header 2. I thought my solution below should work, but apparently not. Can anyone help me out here?

content = """<div>
<p><b>Header 1</b></p>
<p><b>Header 2</b><br></p>
<table>
<tr>
    <td>Something</td>
</tr>
</table>
</div>
"""

from lxml import etree
tree = etree.HTML(content)
tree.xpath("//table/following::p/b[text()='Header 2']")

Answer

paul trmbrth picture paul trmbrth · Oct 9, 2013

Some alternatives to @Arup's answer:

tree.xpath("//p[b='Header 2']/following-sibling::table[1]")

select the first table sibling following the p containing the b header containing "Header 2"

tree.xpath("//b[.='Header 2']/following::table[1]")

select the first table in document order after the b containing "Header 2"

See XPath 1.0 specifications for details on the different axes:

  • the following axis contains all nodes in the same document as the context node that are after the context node in document order, excluding any descendants and excluding attribute nodes and namespace nodes

  • the following-sibling axis contains all the following siblings of the context node; if the context node is an attribute node or namespace node, the following-sibling axis is empty