0

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?

Markus
3,4872 gold badges27 silver badges38 bronze badges
asked Feb 20, 2025 at 16:24
0

1 Answer 1

1

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:

answered Feb 20, 2025 at 17:46
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.