nina_dev
nina_dev

Reputation: 695

How to interpret an XPath?

Can someone please tell me if I have interpreted the following XPath correctly:

 driver.find_elements_by_xpath ("//span[contains(., 'Cardiovascular')]/parent::*/parent::*/descendant-or-self::node()/a/span"
  1. look for the span tags that have "cardiovascular" as part of their text anywhere in the document
  2. Once it is found, check out its immediate parent and all of its descendants or other parents anywhere in the document and the node itself
  3. Then, go to the immediate a tag
  4. Then, go to the immediate span tag?

More specifically, can someone please tell me what /parent::*/parent means? What /parent::/descendant-or-self::node() means? why do we have node() as a function there

Upvotes: 1

Views: 91

Answers (2)

kjhughes
kjhughes

Reputation: 111726

Michael Kay already explained what the XPath selects. Here are the answers to your additional questions:

More specifically, can someone please tell me what /parent::*/parent [sic] means?

  • parent::* means to select the parent element (* matches elements of any name) of the context node.
  • parent::*/parent::* means to select the grandparent element of the context node.

What /parent::/descendant-or-self::node() [sic] means?

why do we have node() as a function there

  • See above for what parent::* means (and note that you forgot the *).
  • descendant-or-self::node() is the same as .//node(); it selects the context node and all of its descendants. Here, node() is not a function but rather a node test. Had it been element(), it would select only elements; comment(), only comments; etc.

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163625

Almost.

Look for the span tags that have "cardiovascular" as part of their text; find their grandparent elements; then within these grandparent elements, find all span elements that have an a element as their parent.

Upvotes: 1

Related Questions