What is Dynamic XPath in Selenium?

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:

  • Basic XPath
  • Using ‘OR’ & ‘AND’
  • Using Contains()
  • Using Starts-With
  • Using Text()
  • Using Index
  • Using Chained XPath
  • XPath Axes
  • Following
  • Following Sibling
  • Preceding
  • Preceding Sibling
  • Child
  • Parent
  • Descendants
  • Ancestors

Example

.//*[@class='product']//h4[contains(text(),'Text')]//ancestor::div[@class='table-good']
/li[@class="b"]//select[@name="s"]//option[contains (text(), 'title')]/preceding-sibling::option
//*[text()=’Text']//ancestor::div[@class='table-goods']