简体   繁体   English

lxml-如何获取元素的最小xpath?

[英]lxml - how to get minimal xpath of element?

tree.xpath("/exact/path/to/element") yields [<Element I want>] . tree.xpath("/exact/path/to/element")产生[<Element I want>]

exact/path/to/element is procured by a call to tree.getroottree().getpath(element) . 通过调用tree.getroottree().getpath(element)获得exact/path/to/element

If I find the minimal xpath to the element with eg Firebug, tree.xpath("//@minimal-descriptor") yields [<Element I want>] . 如果我使用Firebug等找到元素的最小xpath,则tree.xpath("//@minimal-descriptor")产生[<Element I want>]

Question

How do I get the minimal xpath from element using lxml, or other Python library? 如何使用lxml或其他Python库从element获得最小的 xpath?

It is not possible to make lxml generate any xpath expression except the "absolute" one most importantly because there could be enormous amount of xpath expressions pointing to an element. 除了最重要的“绝对”表达式外 不可能使lxml生成任何xpath表达式因为可能会有大量的xpath表达式指向一个元素。

There are also several important points raised here: 这里还提出了几个要点:


As far as I understand from the firebug documentation and the relevant "Split current option "Copy XPath" into two options for copying the absolute and relative XPath" issue , the "Copy Minimal XPath" functionality works only on the elements with id attribute set: 据我从firebug文档和相关的“将当前选项“复制XPath”分为两个用于复制绝对XPath和相对XPath的问题”了解 ,“复制最小XPath”功能仅在设置了id属性的元素上有效:

Copy Minimal XPath - Node with ID - Copies the minimal version of an XPath to the current node to the clipboard 复制最小XPath-具有ID的节点-将XPath的最小版本复制到当前节点到剪贴板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM