Selenium Complex and Dynamic XPath

Selenium XPATH

Selenium XPath using 'AND' operator.

I can assume  that you already know what is xpath , if you don't know you can refer my post What is Xpath , Absolute XPath and Relative XPath?  In selenium automation, if, using other locators, a particular element is not found then XPath is used to identify that particular element. XPath is also very powerful operator to identify complex or dynamic elements. Another reason to use XPath is using it with ‘AND’ operator.  Sometimes, when the user is not able to identify an element with single HTML property because more than one HTML property is required to identify one single element uniquely, the best possible choice is to identify the element is using XPath with ’AND’ operator. Here is an example:

 //tagname[@attribute = '<value>' AND @attribute = '<value>']


 //*[@type='text' and @aria-label='First name' and @class= 'inputtext _58mg _5dba _2ph-']

Note: "*" means all note

Selenium XPath using 'OR' operator

Just like the 'AND', ‘OR’ is also a very powerful operator for identifying a web element. Suppose there is a situation where in a web page, after clicking the ‘submit’ button we are not sure what value it will result in. In this case the ‘success message’ may turn up as 'Successfully saved' or ’Your data saved successfully'. The elements in both the cases are unique in nature, but they might lead to the same result. Here, using the ‘OR’ operator we can avoid this problem.

//tagname[@attribute = '<value>' OR @attribute = '<value>']


 //*[text()='Successfully saved' OR text()='Your data saved successfully']

Note: "*" means all note

XPath to get all child nodes of an element 



The above mentioned XPath will return all the child note of the element whose id is 'reg_form_box'

Few useful XPath 

  1. Contains() function
  2. Starts-with function
  3. Text() function

XPath axes methods

  • Following
  • Ancestor
  • Child
  • Preceding
  • Following-sibling
  • Parent
  • Self
  • Descendant

How to use Contain function in XPath


//ul[@class='featureList' and contains(li, 'Type')]

How to use Starts-with function in XPath


This XPath will find any text which start with "Search" from DOM. If a DOM contains below to HTML line , then this XPath will return both the elements.

<div id = 'username'>Search you name here </div>
<div > Searching your text</div>

How to use Text() function in XPath


This will search for "Delhi" text from complete DOM.

How to use the "Following" axes method function in XPath


This above mentioned expression selects all the elements in the document of the current node( ) where the HTML tag is ‘input’.
If you want to select one particular element then you can use index.


It will select first element .

Share this

Related Posts

Next Post »


March 8, 2020 at 11:55 PM delete

Websites have earned enormous importance in today's world and so the web based applications. Many business establishments are providing different applications on their websites to help customers in doing business with them easily. Selenium Training Online


your ad