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

时间:2021-08-13 07:13:32

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?

    .//* [@id='Passwd']---------如果我们在开始时不使用dot表示什么呢?

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

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

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

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

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

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

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

    //子::输入[@类型= '密码']

4 个解决方案

#1


13  

These expressions all select different nodesets:

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

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

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

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?

如果我们在开始时不使用dot表示什么呢?

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

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

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

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

This would select all element nodes in the whole document.

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

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']

//子::输入[@类型= '密码']

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

这将选择名为input的所有子元素节点,其中@type-attribute-values等于'password'。可以省略child :: axis前缀,因为它是默认行为。

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.

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

#2


21  

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

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

Absolute vs relative XPaths (/ vs .)

绝对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 (ename vs *)

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

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

descendant-or-self axis (//*)

后代或自我轴(// *)

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

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 id attribute value equal to 'Passwd'.
  • .//* [/ id ='Passwd']表示选择当前上下文节点或其下面具有id属性值等于'Passwd'的所有元素。
  • //child::input[@type='password'] can be simplified to //input[@type='password'] and means to select all input elements in the document that have an type attribute value equal to 'password'.
  • // child :: input [@ type ='password']可以简化为//输入[@type ='password'],意味着选择文档中所有输入元素,其类型属性值等于'password' 。

#3


2  

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

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

.//* [@ id ='Passwd']中的*有助于匹配id ='Passwd'的任何元素。

#4


1  

  1. For the first question: It's all about the context. You can see Syntax to know what '.', '..' etc means. Also, I bet you won't find any explanation better than This Link.
  2. 对于第一个问题:这完全取决于背景。你可以看到语法来知道'。','..'等意味着什么。另外,我打赌你不会找到比这个链接更好的解释。
  3. 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.
  4. 第二个问题的简化答案:您通常会使用hd标签找到节点,如td,a,li,div等。但是'*'表示找到符合您给定属性的任何标签。当你确定一个给定的属性而不是关于该元素可能带有的标签时,它主要用于,比如假设我想要一个ID为'xyz'的所有元素的列表,无论它在任何标签中。

Hope it helps :)

希望能帮助到你 :)

#1


13  

These expressions all select different nodesets:

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

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

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

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?

如果我们在开始时不使用dot表示什么呢?

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

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

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

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

This would select all element nodes in the whole document.

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

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']

//子::输入[@类型= '密码']

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

这将选择名为input的所有子元素节点,其中@type-attribute-values等于'password'。可以省略child :: axis前缀,因为它是默认行为。

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.

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

#2


21  

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

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

Absolute vs relative XPaths (/ vs .)

绝对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 (ename vs *)

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

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

descendant-or-self axis (//*)

后代或自我轴(// *)

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

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 id attribute value equal to 'Passwd'.
  • .//* [/ id ='Passwd']表示选择当前上下文节点或其下面具有id属性值等于'Passwd'的所有元素。
  • //child::input[@type='password'] can be simplified to //input[@type='password'] and means to select all input elements in the document that have an type attribute value equal to 'password'.
  • // child :: input [@ type ='password']可以简化为//输入[@type ='password'],意味着选择文档中所有输入元素,其类型属性值等于'password' 。

#3


2  

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

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

.//* [@ id ='Passwd']中的*有助于匹配id ='Passwd'的任何元素。

#4


1  

  1. For the first question: It's all about the context. You can see Syntax to know what '.', '..' etc means. Also, I bet you won't find any explanation better than This Link.
  2. 对于第一个问题:这完全取决于背景。你可以看到语法来知道'。','..'等意味着什么。另外,我打赌你不会找到比这个链接更好的解释。
  3. 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.
  4. 第二个问题的简化答案:您通常会使用hd标签找到节点,如td,a,li,div等。但是'*'表示找到符合您给定属性的任何标签。当你确定一个给定的属性而不是关于该元素可能带有的标签时,它主要用于,比如假设我想要一个ID为'xyz'的所有元素的列表,无论它在任何标签中。

Hope it helps :)

希望能帮助到你 :)