Showing posts with label xpath examples. Show all posts
Showing posts with label xpath examples. Show all posts

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:
Syntax

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

 Example

 //*[@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.
Syntax

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


 Example

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

Note: "*" means all note

XPath to get all child nodes of an element 

 Example

//*[@id='reg_form_box']/child::*

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


//*[contains(text(),'Search')]

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



How to use Starts-with function in XPath

//*[starts-with(text(),'Search')]

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

//*[text()='Delhi']

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

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

//*[@id="ch_login_email"]//following::input

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.

 //*[@id="ch_login_email"]//following::input[1]

It will select first element .