简体   繁体   中英

What is the meaning of :: in selenium

What is the exact meaning of :: ? And apart from parent, what else are the different things we can use?

By.xpath("parent::*/parent::*")

The shortest answer I can manage

:: separates an axis name from a node test in an XPath expression.

The longer answer

It does not make much sense to ask about the meaning of " :: in Selenium", because it's not a feature of Selenium. It belongs to XPath, which is a W3C specification in its own right and is used to navigate XML or XHTML documents.

By.xpath("   parent::*/parent::*    ")
    ^                 ^             ^
Selenium            XPath        Selenium

Selenium just happens to embed XPath in their web application framework (which is a good thing!).

So, I've taken the liberty to answer the question: What is the meaning of :: in XPath?

The meaning of :: in XPath

In XPath, :: does not mean anything on its own and only makes sense if there is

  • a valid XPath axis identifier to the left
  • a valid node test to the right

For example, parent::* is a valid XPath expression 1 . Here, parent is an XPath axis name, * is a node test 2 - and :: marks the transition from the axis to the node test. Other possible axes are

ancestor              following-sibling
ancestor-or-self      namespace
attribute             parent
child                 preceding
descendant            preceding-sibling
descendant-or-self    self
following

Of course those are not just names, they have a very clear-cut semantic dimension: each of them defines a unique way to navigate an XML document (or, rather, a tree-like representation of such a document). Their meaning is straightforward in most cases, for instance, following:: identifies something that "follows" the current context.

These tuples of axis and node test (or triples, also counting predicates) can be "chained together" with the binary / operator to form paths with several steps :

outermost-element/other/third

Navigating a simple document

<root>
    <person>James Clark</person>
    <person>Steve DeRose</person>
</root>

Naturally, navigation might depend very much on your current whereabouts. There are both absolute and relative path expressions. An example for an absolute path expression is

/child::root/child::person      | abbreviated syntax:     /root/person

As you can see, there is a / at the beginning of an absolute path expression. It stands for the document node (the outermost node of a tree, which is different from the outermost element node of aa tree). Relative path expressions look like

child::person                   | abbreviated syntax:     person

The relative path expression will only find the person element node if the current context is the root element node. Otherwise, it will fail to locate anything.

Your XPath expression

To sum up and use what we have learned so far:

By.xpath("parent::*/parent::*")

finds the element node that is the grandparent of the current node. The names of both the parent and the grandparent node do not matter (that's what * is for). There's no / at the beginning, so it must be a relative path.


1 In fact, it is a location path , a special kind of XPath expression. Also, I have left out one important concept: predicates . Good things always come in threes, and XPath expressions come with an axis, a node test and with zero or more predicates.

2 A node test must be either a name test (testing the name of a node) or a kind test (testing the kind of node). Find ample information about node tests in the relevant part of the XPath specification .

This is xpath syntax, you can do other things like :

child::*            Selects all element children of  current node
attribute::*        Selects all attributes of  current node
child::text()   Selects all text node children of  current node
child::node()   Selects all children of current node

Check a tutorial, especially about axes :

http://www.w3schools.com/xpath/xpath_axes.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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