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.
Use getpath from ElementTree objects.
from lxml import etree root = etree.fromstring(''' Datadata data ''') tree = etree.ElementTree(root) for e in root.iter(): print(tree.getpath(e))
Prints
/foo /foo/bar[1] /foo/bar[2] /foo/bar[2]/baz[1] /foo/bar[2]/baz[2]