Shane Cleary

On t'internet!

XPath

Finding elements with XPath

Find Element using XPath

XPath is the standard navigation tool for XML

and an HTML document is also an XML document (xHTML)

Used when no element Id or name. We have to dig down to find the element


Absolute Xpath (Don't use)

Easiest way to identify

Cons: Long, Any change in the structure will break the locator


Relative XPath

Cleaner way to identify elements

More stable

Cons: Takes more time

/ Selects from the root node

// Selects nodes in the document from the current node that match the selection no matter where they are

. Selects the current node

.. Selects the parent of the current node

@ Selects attributes

Example

//input[@value='XPath Button 2']


Using attributes in xpath

//*[@id='']

// - go through all the nodes

* - regardless of the tag (Wild card char - very useful for skipping through code)

@id= - at id attribute equal to


//xpath href attribute

//[@href='/icon-sucess/]


How to identify a button

Using class: //*[@class='cvb '] (may not be unique)

//a[contains(text(),'Click Me')]

xpath function 'contains' and text()

//span[contains(text(), 'Email')]


Axes View

SpecFlow

Axis typeMatches
Ancestor All nodes above the context node, including the parent, grandparent, and so on up to the root node.
Ancestor-or-self The ancestor node plus the context node.
Attribute Attributes of the context node.
Child Children of the context node.
Descendant Children of the context node, plus their children, and so on down to the leaves of the subtree.
Descendant-or-self The descendant node plus the context node.
Following Nodes that follow the context node at any level in the document. This does not include any descendants of the context node, but does include its following siblings and their descendants.
Following-sibling Nodes that follow the context node at the same level (i.e., that share the same parent as thecontext node).
Namespace All the namespace nodes of an element.
Parent The parent of the context node.
Preceding Nodes that occur before the context node at any level in the document. This does not include any descendants of the context node, but does include its preceding siblings and their descendants.
Preceding-sibling Nodes that occur before the context node at the same level (i.e., that share the same parent as the context node).
Self The context node itself.

Descendant Axes View

Finding nodes in a hierarchy.

Select all children of the node
//form[id@’Form’]/child::*

Select all children of type input
//form[id@’Form’]/child::input

Select a particular input
//form[id@’Form’]/child::input[@class=’’]
//*[@id='AddDocumentReviewModalPopup']//child::input[@id='Title']

Select all descendant nodes (child and grandchild)
//*[@id='AddDocumentReviewModalPopup']//descendant::*

Select the first node of type ‘legend’
//*[@id='AddDocumentReviewModalPopup']//descendant::legend[position()=1]

		

XPath query to get nth instance of an element.


//Select the nth instance of an element
(//input[@id="search_query"])[2]

//For loop to click on every instance of the class and then take a screenshot
for (int i=1; i < 12; i++)
{
      Driver.FindElement(By.XPath("(//*[@class='more-details fa fa-question-circle'])[" + i + "]")).Click();

      Screenshot ss = ((ITakesScreenshot)Driver).GetScreenshot();
      ss.SaveAsFile("DPA Tooltip "+i+".png");
}

Footer