I\'m searching in a HTML document using XPath from lxml in python. How can I get the path to a certain element? Here\'s the example from ruby nokogiri:
page.
See the Xpath and XSLT with lxml from the lxml documentation This gives the path of the element containg the text
An example would be
import cStringIO
from lxml import etree
f = cStringIO.StringIO('hello world ')
tree = lxml.etree.parse(f)
find_text = etree.XPath("//text()")
# and print out the required data
print [tree.getpath( text.getparent()) for text in find_text(tree)]
# answer I get is
>>> ['/foo/bar/x1[1]', '/foo/bar/x1[2]']