Ξένη Γήινος
Ξένη Γήινος

Reputation: 3072

Python lxml.html SyntaxError: invalid predicate with XPATH when using lxml find

I am using CPython 3.12.6, lxml 5.3.1, Windows 11 Pro 23H2 x64.

The following Python code raises an exception:

tree.find(".//table[contains(@class, 'wikitable')]//tr")
SyntaxError: invalid predicate

Interestingly the following works:

tree.xpath(".//table[contains(@class, 'wikitable')]//tr")

Why?

I am trying to understand why in this case, using lxml.html library, using the same XPATH, invoking .find with it on an lxml.html.HtmlElement object raises an exception, but invoking .xpath with the exact same XPATH on the same object succeeds. Aren't they supposed to be the same?

Upvotes: 0

Views: 26

Answers (1)

mzjn
mzjn

Reputation: 51002

find() is not the same as xpath().

The find() method comes from the ElementTree API, which only supports a subset of XPath 1.0. contains() is one of the unsupported features.

More information:

Upvotes: 1

Related Questions