Search code examples
pythonxpathlxml

Lxml html xpath context


I'm using lxml to parse a HTML file and I'd like to know how I can set the context of xpath search. What I mean is that I have a node element and want to make xpath search only inside this node as if it was the root one. For example, I have a form node and xpath search //input returns only inputs of the given form as opposed to all inputs of all forms on the page. How can I do this? I've found some xpath context docs here, but it doesn't seem to be quite what I want.


Solution

  • XPath expression //input will match all input elements, anywhere in your document, while .//input will match all inside current context.

    Maybe if you improve your scenario description we can help you further.