How Can We Move To Parent of an Element Using XPath?

Timothy Joseph | October 26, 2020

How Can We Move To Parent of an Element Using XPath?

XPath

When creating locators in automation testing, many times we come across situations when elements are not found by their id, class, name etc. In this case, you can use XPath to locate these elements. There are two ways to set up the XPath, one is with forward single slash ‘/ ‘ and another is using forward double slash ‘// ‘.

Single slash '/' : Single slash is also known as the absolute path and traverses directly from parent to child in DOM driver.findElement(By.xpath("/html/body/div/input"))

Double slash '// ' : Double slash is also known as the relative path and finds the matching element in every corner of DOM.
driver.findElement(By.xpath("//input[@name=’Tutorial’]")).

Syntax for xpath : //tagname[@attribute=’value’] or //*[@attribute=’value’]

Following example can be considered:

Xpath

You can use the steps below steps to create your own XPath:

Create the path for the element //div[class='component-article-section-content ui-body content-section-0']

Use the parent axes with the parent node's name to move to parent of an element //div[class='component-article-section-content ui-body content-section-0']/parent::div

Disclaimer

This publication is for informational purposes only, and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.

Post a Comment

Categories