xml XPath 中的 .// 和 //* 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35606708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:11:06  来源:igfitidea点击:

What is the difference between .// and //* in XPath?

xmlseleniumxpathselenium-webdriverautomated-tests

提问by TEST-M

While finding the relative XPath via Firebug : it creates like

通过 Firebug 查找相对 XPath 时:它创建如下

  1. .//*[@id='Passwd']--------- what if we dont use dot at the start what it signifies?

  2. Just add //*in the Xpath -- it highlights --- various page elements ---------- what does it signify?

  1. .//*[@id='Passwd']--------- 如果我们在开头不使用点会怎样,它的含义是什么?

  2. 只需添加//*Xpath -- 它突出显示 --- 各种页面元素 ---------- 它表示什么?

Below are XPaths for Gmail password fields. What is significance of *?

以下是 Gmail 密码字段的 XPath。有什么意义*

  • .//*[@id='Passwd']

  • //child::input[@type='password']

  • .//*[@id='Passwd']

  • //child::input[@type='password']

回答by kjhughes

There are several distinct, key XPath concepts in play here...

这里有几个不同的、关键的 XPath 概念……

Absolute vs relative XPaths (/vs .)

绝对与相对 XPath ( /vs .)

  • /introduces an absolute location path, starting at the root of the document.
  • .introduces a relative location path, starting at the context node.
  • /引入一个绝对位置路径,从文档的根开始。
  • .引入相对位置路径,从上下文节点开始。

Named element vs any element (enamevs *)

命名元素 vs 任何元素 ( enamevs *)

  • /enameselects an enameroot element
    • ./enameselects all enamechild elements of the current node.
  • /*selects the root element, regardless of name.
    • ./*or *selects all child elements of the context node, regardless of name.
  • /ename选择一个ename根元素
    • ./ename选择ename当前节点的所有子元素。
  • /*选择根元素,而不考虑名称。
    • ./**选择上下文节点的所有子元素,无论名称如何。

descendant-or-self axis (//*)

后代或自身轴 ( //*)

  • //enameselects all enameelements in a document.
    • .//enameselects all enameelements at or beneath the context node.
  • //*selects all elements in a document, regardless of name.
    • .//*selects all elements, regardless of name, at or beneath the context node.
  • //ename选择ename文档中的所有元素。
    • .//ename选择ename上下文节点处或下方的所有元素。
  • //*选择文档中的所有元素,而不考虑名称。
    • .//*选择上下文节点处或下方的所有元素,无论名称如何。


With these concepts in mind, here are answers to your specific questions...

考虑到这些概念,以下是您特定问题的答案......

  • .//*[@id='Passwd']means to select all elements at or beneath the current context node that have an idattribute value equal to 'Passwd'.
  • //child::input[@type='password']can be simplified to //input[@type='password']and means to select all inputelements in the document that have an typeattribute value equal to 'password'.
  • .//*[@id='Passwd']表示选择id属性值等于 的当前上下文节点处或下方的所有元素'Passwd'
  • //child::input[@type='password']可以简化为 //input[@type='password']和 意味着选择input文档中type属性值等于 的所有元素'password'

回答by zx485

These expressions all select different nodesets:

这些表达式都选择不同的节点集:

.//*[@id='Passwd']

.//*[@id='密码']

The '.' at the beginning means, that the current processing starts at the current node. The '*' selects all element nodes descending from this current node with the @id-attribute-value equal to 'Passwd'.

这 '。' 在开始意味着,当前处理从当前节点开始。'*' 选择从这个当前节点降序的所有元素节点,其@id-attribute-value 等于 'Passwd'。

What if we don't use dot at the start what it signifies?

如果我们不在开头使用点,它代表什么?

Then you'd select all element nodes with an @id-attribute-value equal to 'Passwd' in the wholedocument.

然后,您将选择整个文档中@id-attribute-value 等于 'Passwd' 的所有元素节点。

Just add //* in the XPath -- it highlights --- various page elements

只需在 XPath 中添加 //* -- 它突出显示 --- 各种页面元素

This would select all element nodes in the wholedocument.

这将选择整个文档中的所有元素节点。

Below mentioned : XPatht's for Gmail Password field are true what is significance of * ?

下面提到:XPatht 的 Gmail 密码字段是真的 * 的意义是什么?

.//*[@id='Passwd']

This would select all element nodes descending from the current node which @id-attribute-value is equal to 'Passwd'.

这将选择从当前节点降序的所有元素节点,其中@id-attribute-value 等于 'Passwd'。

//child::input[@type='password']

//child::input[@type='password']

This would select all child-element nodes named inputwhich @type-attribute-values are equal to 'password'. The child::axis prefix may be omitted, because it is the default behaviour.

这将选择名为所有子元素节点input,其@type-attribute值等于“密码”。该child::轴前缀可以被省略,因为它是默认的行为。

The syntax of choosing the appropriate expression is explained here at w3school.com.

在 w3school.com上解释了选择适当表达式的语法。

And the Axes(current point in processing) are explained here at another w3school.com page.

轴(处理中的当前点)在另一个 w3school.com 页面上进行了解释。

回答by alecxe

The dot in XPath is called a "context item expression". If you put a dot at the beginning of the expression, it would make it context-specific. In other words, it would search the element with id="Passwd"in the context of the node on which you are calling the "find element by XPath" method.

XPath 中的点称为“上下文项表达式”。如果您在表达式的开头放一个点,它将使其特定于上下文。换句话说,它将id="Passwd"在您调用“通过 XPath 查找元素”方法的节点的上下文中搜索元素。

The *in the .//*[@id='Passwd']helps to match any elementwith id='Passwd'.

*.//*[@id='Passwd']帮助匹配任何元素id='Passwd'

回答by Prateek

  1. For the first question: It's all about the context. You can see Syntaxto know what '.', '..' etc means. Also, I bet you won't find any explanation better than This Link.
  2. Simplified answer for second question: You would generally find nodes using the html tags like td, a, li, div etc. But '*' means, find any tag that match your given property. It's mostly used when you are sure about a given property but not about that tag in which the element might come with, like suppose I want a list of all elements with ID 'xyz' be it in any tag.
  1. 对于第一个问题:这完全取决于上下文。您可以查看语法以了解“.”、“..”等的含义。另外,我敢打赌,您找不到比This Link更好的解释了。
  2. 第二个问题的简化答案:您通常会使用 td、a、li、div 等 html 标签来查找节点。但“*”表示查找与您的给定属性匹配的任何标签。它主要用于当您确定给定属性而不是元素可能附带的标签时,例如假设我想要一个 ID 为“xyz”的所有元素的列表,无论是在任何标签中。

Hope it helps :)

希望能帮助到你 :)