Home Contact us Privacy plicy About us DMCA

Let's learn Xpath (Software Testing)

Note- For Running below Xpath's, please use https://www.google.co.in/ OR Below given table.

1. What is xpath?

Xpath is an expression language which is used to find out the element from xml document. These are nothing but the web elements. In selenium, the xpath of the element are used to take any action on the element.

2. Type of xpath

2.1 Relative xpath

Relative xpath starts from the middle of DOM. It start with double slash(//), which mean it can search from anywhere in DOM.

2.2 Absolute xpath

Absolute xpath start from the root of element to get the desire result. If there is any minor change in DOM structure then absolute xpath will not work, hence people mostly use Relative xpath to avoid xpath related issues.

3. Basic of xpath writing

3.1 /(Single slash) –

This is used to create absolute xpath. This will helpful to find immediate child node of you current node.

3.2 //(double slash)-

This is used to find the node anywhere from the DOM where it matches.

3.3 Tagname-

Tag name of the current node.

3.4 @ (at the rate)-

This is used to select the attribute.

3.5 Attribute-

Attribute name of the node.

3.6 Value-

Value of the attribute.

3.7 .(dot) –

This is used to select current node.

3.8 ..(double dot)-

This is used to select parent node.

3.9 *(star)-

This represent any. Such as any node, any attribute etc.

4. Ways of writing xpath

4.1 Single attribute-

Here we have use single attribute to find the element. Below is the example of single attribute.

.//div[(@id='SIvCob')]//a[ (contains(text(),'हिन्दी'))]

In above example, 'id and text is the sinle attribute in each tag.

4.2 Multiple attribute –

Here we have used multiple attribute (two or more) to find the elements from single tags. Below is the example of multiple attribute where as a condition we have mentioned two attribute name from one tags to find out the element

.//input[contains(@value,'Feeling')][contains(@type,'submit')]

4.3 contains() –

‘contains’ operator is to check for any matching text available in the attribute value. Below is the example of contains.

.//div[(@id='SIvCob')]//a[ (contains(text(),'हिन्दी'))]

4.4 Not contains() –

This function is used to find the element which excludes the text which is mentioned in the search criteria.

.//h3[contains(text(),'XPath')]

4.5 starts-with() –

starts-with() function work same like contains(), the only difference is, this function match the starting text of the element and fetch the result.

.//h3[starts-with(text(),'Xpath')]

4.6 text() –

text() attribute is used to find text elements in DOM.

.//div[contains(@id,'SIvCob')]//text() .//div[contains(@id,'SIvCob')]//a[text()='हिन्दी']

4.7 last() –

This function find last element from the list of elements.

.//div[contains(@id,'SIvCob')]//a[last()] .//div[contains(@id,'SIvCob')]//a[last()-1]

4.8 position() –

This function is used to find specified positioned element from the list of elements.
.//div[contains(@id,'SIvCob')]//a[position()=4] .//div[contains(@id,'SIvCob')]//a[position()>=4] .//div[contains(@id,'SIvCob')]//a[position()<=4] .//div[contains(@id,'SIvCob')]//a[position()>4]
Fetching last 5 records
.//div[@id='SIvCob']//a[position()>=last()-4 and position()<=last()]


4.9 index()–

This function is same as position() function where index number can be mentioned to get specific element.

.//div[contains(@id,'SIvCob')]//a[1]

4.10 Following xpath –

This function is used to find out immediate next elements of the component.

.//div[contains(@id,'SIvCob')]//a[contains(text(),'मराठी')]//following::a

4.11 Preceding xpath -

This function is used to find out immediate preceding elements of the component.

.//div[contains(@id,'SIvCob')]//a[contains(text(),'मराठी')]//preceding::a

4.12 Following-sibling-

It will give all the following records which match the search criteria, result belongs to same parents nodes, as we are using sibling.

.//div[contains(@id,'SIvCob')]//a[contains(text(),'ગુજરાતી')]//following-sibling::a

In above example, we are trying to find out all the list of element after ‘Gujarat’ which are sibling to these element.


4.13 Preceding-sibling-

It will give all the preceding records which match the search criteria, result bellows to same parents nodes as we are using sibling.

.//div[contains(@id,'SIvCob')]//a[contains(text(),'मराठी')]//preceding-sibling::a

4.14 Child –

This function is used to find immediate following element within the selected tag.

.//div[contains(@class,'Fgvgjc')]//child::style

4.15 Parent(/..)

This function can be used to get the parent tag.

.//div[contains(@class,'Fgvgjc')]//parent::body .//div[contains(@class,'Fgvgjc')]//..

4.16 Ancestors-

This function is use to select all parent nod from the top.

.//div[contains(@id,'SIvCob')]//ancestor::div

4.17 Descendants –

This function is used to get child and grandchild of following nod.

.//div[contains(@class,'L3eUgb')]//descendant::a

4.18 Pipe line”|” –

This function is used to execute adding xpath query to get the multiple result.

.//div[contains(@class,'Ne6nSd')] | //div[contains(@class,'LLD4me')]

4.19 Nested block-

In case of nested block, you can write hierarchy of one node in block to meet the condition.

.//div[contains(@class,'3j99')][div[contains(@class,'LX3sZb')]//a[contains(text(),'Gmail')]]

4.20 translate-

This function is used to ignore case. So if the letter is in lower or upper case, we can ignore the case and find out the elements.
.//*[translate(text(),'G','g')='google offered in: ']

5. Mathematics Logical Operator

5.1 Addition(+)

Addition operator is used for adding 2 or more number togather. In below xpath, we have used his operator to add 2 number to find out the positin of element.

.//div[@id='SIvCob']//a[position()>=1+1]

5.2 Minus(-)

Minus operator is used to substract 2 number. In below xpath, we use this operator to find out last-first element from the list of element, which is last element -1.

.//div[contains(@id,'SIvCob')]//a[last()-1]

5.3 Multiplication(*)

Multiplication operator is used to multiply two numbers. Xpath example given below.

.//div[@id='SIvCob']//a[position()>=2*2]

5.4 Div

Here we need to understand how "Div" operator work. Let's take one example, We have list of 6 elements, So if we divide 6 by 2 then the answer will be 3, and when we divide from 3 then answer will be 2. So if we have select 6 element with the help of "div" operator then we will right div 2=3 Or div 3=2. Below xpath will fetch 4 element from the list

.//div[@id='SIvCob']//a[position() div 2=2]

5.5 Equal(=)

"=" operator is used to find out any specific text or element. In below xpath example, we are searching for hindi language with the help of equal to operator.

.//div[@id='SIvCob']//a[(text()='हिन्दी')]

5.6 Not Equal(!=)

"!=" Not equal to operator is used to exclude any specific element. In below xpath example, we exclude hindi language.

.//div[@id='SIvCob']//a[(text()!='हिन्दी')]

5.7 Less than(<)

Less than operator is used to find out the list of elements less than the given number. This operator can be used in other different way. In below example, We are finding element less then given number i.e. 2.

.//div[@id='SIvCob']//a[position()<2]

5.8 Less than equalTo(<=)

Less than operator is used to find out the list of elements less than and equal to the given number. In below example, We are finding element less then given equal to number i.e. 2. So here it will include second element which finding the elements.

.//div[@id='SIvCob']//a[position()<=2]

5.9 Greater Than(>)

Greater than operator is used to find out the list of elements greater than the given number. In below example, We are finding element greater then given number i.e. 2.

.//div[@id='SIvCob']//a[position()>2]

5.10 Greater Than equalTo(>=)

Greater than operator is used to find out the list of elements greater than and equal to the given number. In below example, We are finding element greater then and equal to the given number i.e. 2. In the result, it will include given number as well.

.//div[@id='SIvCob']//a[position()>=2]

5.11 Or

Or operator is conditional operator where it will find the element when any of the condition are true. If both the condition are true then it will show 2 result.

.//div[@id='SIvCob']//a[contains(text(),'हिन्दी') or contains(text(),'मराठी')]

5.12 AND

AND operator is conditional operator, where it will find the element when 2 or more condition are true. Such as in below scenario, we are putting 2 element conditon to find out specific element.


5.13 MOD

MOd operator is modulus operator. In below example, it will find the list element which start from 2 and next element will be of 4th place. So if you write Mod 3=1 then it will find the element from first and next element will third and so on.

.//div[@id='SIvCob']//a[position() mod 4=2]

Let's Practice Some Xpath Here

Name Code Date Time
Amazon 20 20/10/2021 10.11
ebay 21 12/09/2018 11.17
Shopify 22 16/04/2022 09.20
Netflix 24 18/07/2013 07.09

Country:- India Japan USA UK Germany Australia canada Oman France Spain
abc

6. String Function Logic

6.1 string

String function is used to covert any type of datatype in string. Refer above table for xpath

.//table[@id='xpath']//td[string(text())='20']

6.2 concat

Concat function is used to concate two attribute value togather and find out the result.

.//input[concat(@class,@value)='gNO89bGoogle Search']

6.3 start-with Refer 4.5
6.4 contains Refer 4.3

6.5 substring(string, number1, number2)

Substring is used to find out some part of string value from te actual text.

.//div[substring(text(),1,4)='Goog'] //div[substring(@class, string-length(@class) - 4) = 'LS8OJ']

6.6 substring-before

substring-before is used to find out the text before any specific text or symbol.

.//input[substring-before(@value,' ')='Google']

6.7 substring-after

substring-after is used to find out the text after any specific text or symbol.

.//input[substring-after(@value,' ')='Search']

6.8 string-length

string length is used to find out the text of any specific length.

.//input[(@value=string-length('Google Search')>12)]

6.9 normalize-space

This function will remove unwanted space or new line and fetch the elments.
For Removing space .//table[@id='xpath']//span[normalize-space(text())='USA']
For removing new line character .//table[@id='xpath']//span[normalize-space()='Spain abc']

7. Numaric operator Logic

7.1 number

This will convert anytype in number. Refer above table for xpath
.//table[@id='xpath']//td[number(text())='10.11']

7.2 floor

It will return the largest which is less than or equal to number. Refer above table for xpath
.//table[@id='xpath']//td[floor(text())='10']

7.3 celling

It will return the nearest smallest integer value. Refer above table for xpath
.//table[@id='xpath']//td[ceiling(text())='10']

7.4 round

It will return the integer nearest in the value to number. Refer above table for xpath
.//table[@id='xpath']//td[round(text())='10']

7.5 count

This will check the count value and fetch the list of elements match with this count. Such as in below example, it will check tr tag which has 5 time text element present. Refer above table for xpath
.//table[@id='xpath']//tr[count(text())='5']

8. Boolean Operator Logic

8.1 Boolean (anytype)

This use to convert anytype to boolean value true or false. It will give you the result in true or false.
.//div[boolean(@id='SIvCob')]

8.2 Not (Boolean)

Not opertor is to give boolean value as true if the conditions false and false when the condition is true It will give all input record once the string-length condition is true.
.//input[not(@value=string-length('Google Search')<14)]

8.3 True()

This function will check if the condition is true, if yes then it will fetch only those record where the condition is true.
.//input[contains(@value,'Google Search')=true()]

8.4 False()

This function will search the elment which are other than it's mentioned in the condition. Below xpath will provide list of input element which doesn't containt @value as 'Google Search'.
.//input[contains(@value,'Google Search')=true()]

9. XPath frequently asked Interview Questions

a) What is difference between Relative and absolute Xpath.

b) Which xpath is mostly used relative or absolute? and why?

c) What is sibling?

d) How to use following and preceding sibling work?

Find out Min and Max element number via xpath ()

e) Find out the Greatest number from the element. Refer above table for xpath.
.//table[@id='xpath']//td[2][not((text()) < //td[2])]

Screen shot











f) Find out the Smallest number from the element. Refer above table for xpath.
.//table[@id='xpath']//td[2][not((text()) > //td[2])]

Screen shot











d) What are the Comparison Operator?