Software Development and QA Tips By QASource Experts

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

Written by Timothy Joseph | Oct 26, 2020 4:00:00 PM

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:

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