[issue42893] Strange XPath search behavior of xml.etree.ElementTree.Element.find

Christian Heimes report at bugs.python.org
Wed Jan 13 05:32:02 EST 2021


Christian Heimes <lists at cheimes.de> added the comment:

etree's find method supports a limited subset of XPath, https://docs.python.org/3/library/xml.etree.elementtree.html#supported-xpath-syntax . e.find("./*[2]") seems to trigger undefined behavior. The limited XPath syntax for positions is documented as "position predicates must be preceded by a tag name".

lxml behaves the same. Its find() method returns the same value and its xpath() method your expected value:

>>> import lxml.etree
>>> e = lxml.etree.fromstring('<html><div class="row"/><hr/><div/><hr/><div class="row"/><button/></html>')
>>> e.find("./*[2]")
<Element div at 0x7fe4d777b6c0>
>>> e.xpath("./*[2]")
[<Element hr at 0x7fe4d777b2c0>]

----------
nosy: +christian.heimes, scoder

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42893>
_______________________________________


More information about the Python-bugs-list mailing list