1

What am I doing wrong?

When using what I believe to be a valid XPath expression in Python, I get

SyntaxError: invalid predicate

My code is:

import xml.etree.ElementTree as ET
data = '''<root>
            <child>
                <p>aaa</p>
                <p>bbb</p>
            </child>
        </root>'''

root = ET.fromstring(data)

p_element = root.findall(".//p[text()='aaa']")

print(p_element[0].text)

Running this code produces an invalid predicate error, but I believe it is correct. I'm using Python version 3.10.0, and running this on Windows.

It seems to have something to do with the text() part of the XPath; with that removed, it does not error.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
anon
  • 87
  • 1
  • 10
  • Try `"//p[contains(., 'aaa')]"` – NotAName May 11 '23 at 01:02
  • `//p[. = 'aaa']` could work.OP's is indeed and invalid expression on ElementTree module: see: https://docs.python.org/3/library/xml.etree.elementtree.html#xpath-support – LMC May 11 '23 at 01:14

1 Answers1

2

.//p[text()='aaa'] is a perfectly valid XPath expression.

The problem is that ElementTree's implementation of XPath is incomplete.

For your XML, testing the string value, .//p[.='aaa'], would be a work-around if you're stuck with ElementTree. Better would be to only use an XPath library such as lxml that better follows the standard.

See also

kjhughes
  • 106,133
  • 27
  • 181
  • 240