XPath locators in Selenium Webdriver

If you have been using webdriver you would know the paramount nature of Xpath! I have a lot of colleagues who start googling every-time they get stuck with trying to find an element.

Some classic examples are:

Xpath to find the occurrence of a webelement with same properties or attributes:
We are finding the third occurrence where the name attribute is Test
Remember: The [] operator has higher precedence (priority) than the // abbreviation.

If the text is not exact match. Try following:

Radio Buttons - //input[@value='2']

iFrames - //iframe[@id='iframePage']

Here is an exhaustive list that will help you for sure:

Whole web page xpath=/html 

Whole web page body xpath=/html/body
Element with id 'Test' 
//Element [@id='test'] 
Any element with id test

Second element anywhere on page

Image element //img
Element with attribute containing text 'test'

Element whose attribute starts with 'test'
//Element[starts-with(@attribute, 'test')] 

Element1 with id A or element2 with id B
//Element1[@id='A'] | //Element2[@id=B] 

Element with id A or id B
//E1[@id='A' or @id='B']

Element with name N & specified value ‘v’ 

Link element //a 

containing text 't' exactly

with target link 'url'

First child of element  
First child 

Last child of element E 

Last child 

Element with no children 
Element with an only child 

Cell by row and column (e.g. 3rd row, 2nd column) 
//*[@id='TestTable'].2.1 } 
Cell immediately following cell containing 't' exactly 
Cell immediately following cell containing 't' 

User interface element that is disabled 
User interface element that is enabled 
Checkbox (or radio button) that is checked 
Enjoy XPathing :) 


Popular posts from this blog

XPATH for IE / internet explorer

RPA - Blue Prism, OpenSpan, Automation Anywhere vs UIPath