dynamic Xpath

As we have described before how to find XPath in the browser, here we are going to describe different dynamic XPath functions.

What Is Dynamic XPath In Selenium?

XPath, or XML Path, is one of Selenium WebDriver’s most commonly used locators for navigating a page’s HTML structure. It can locate any element in a web page using HTML DOM structure in HTML and XML documents.

XPath allows XML document navigation to select individual elements, attributes, or other parts of an XML document for specific processing. For example, XPath generates reliable locators but is slower in terms of performance than CSS Selector.

The language XPath is used to select elements in an HTML page. Using XPath, you can find any element on a page based on its tag name, ID, CSS class, etc. In Selenium, there are two types of XPath. Dynamic XPath is also called as custom XPath and it is one way to locate element uniquely.

Dynamic XPath is used to locate exact attribute or decrease the number of matching nodes/result from a webpage and following XPath expressions can be used for the same:

  • Contains
  • Sibling
  • Ancestor

How to find XPath for dynamic elements in selenium?

XPath axes from the current node are utilized to locate dynamic elements in Selenium, allowing the search for nodes in the XML document. This is crucial for finding nodes closest to the tree. XPath axes provide methods for locating dynamic elements that standard XPath methods might miss, especially when identifiers like ID, Classname, or Name are absent.

In Selenium WebDriver, axes methods such as child, parent, ancestor, sibling, preceding, self, etc., are commonly employed to handle dynamically changing elements.

Modifying test scripts due to changes in the Application Under Test (AUT) is a challenging task in automation testing. Developers frequently alter identifiers, and elements may dynamically change during execution.

Automation testers should avoid setting fixed XPaths for test case elements to overcome these challenges. Instead, they should dynamically script XPaths based on specific patterns, ensuring adaptability to changes in the AUT.


Contains is used to locate the web element who matches the specific text from multiple blocks.  

As per the below image, if in your web page, there are sections that have the same element for all row, then you can find the specific element by using Contains to select text in that row.

Example: .//*[@class='product']//h4[contains(text(),'Text')]//ancestor::div[@class='table-good'] .//*[@class='product']//h4[contains(.,'Text')]//ancestor::div[@class='table-good']


As the meaning of sibling, we can use this to find an element which is related to some other element. There are basically two types of sibling function which are used in XPath.

A) Preceding Sibling – If we select one sibling from given list, the “preceding sibling” function takes the preceding options of the selected one.


//ul[@class="OPENCR_flex_containerleft"]//li[@class="OPENCR_flex_box-other"]//select[@name="sort"]//option[contains (text(), 'by title')]/preceding-sibling::option

B) Following Siblings – If we select one sibling from given list, the “following sibling” function takes the following options of selected one. Example:

//ul[@class="OPENCR_flex_containerleft"]//li[@class="OPENCR_flex_box-other"]//select[@name="sort"]//option[contains (text(), 'by title')]/following-sibling::option


We can use this to find an element on basis of the parent element. As per below image, if in your web page there is a section which has a same element for all the rows then you can find one row element by using ancestor.

Floating Icon 1Floating Icon 2