Let's learn Xpath (Software Testing)
1. What is xpath? 2. Type of Xpath 3. Basics of Xpath writing 4. Ways of writing xpath (Axes) 5. Mathematics Logical Operator 6. String Function Logic 7. Numeric operator Logic 8. Boolean Operator Logic 9. Interview questions
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 xpathRelative xpath starts from the middle of DOM. It start with double slash(//), which mean it can search from anywhere in DOM.
2.2 Absolute xpathAbsolute 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
This is used to create absolute xpath. This will helpful to find immediate child node of you current node.
This is used to find the node anywhere from the DOM where it matches.
Tag name of the current node.
This is used to select the attribute.
Attribute name of the node.
Value of the attribute.
This is used to select current node.
This is used to select parent node.
This represent any. Such as any node, any attribute etc.
4. Ways of writing xpath
4.1 Single attribute 4.2 Multiple attribute 4.3 contains() 4.4 Not contains() 4.5 starts-with() 4.6 text() 4.7 last() 4.8 position() 4.9 index() 4.10 Following Xpath 4.11 Preceding Xpath 4.12 Following-sibling 4.13 Preceding-sibling 4.14 Child 4.15 Parent(/..) 4.16 Ancestors 4.17 Descendants 4.18 Pipe line (|) 4.19 Nested block 4.20 translate
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.
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')]‘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(),'हिन्दी'))]This function is used to find the element which excludes the text which is mentioned in the search criteria.
.//h3[contains(text(),'XPath')]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')]text() attribute is used to find text elements in DOM.
.//div[contains(@id,'SIvCob')]//text() .//div[contains(@id,'SIvCob')]//a[text()='हिन्दी']This function find last element from the list of elements.
.//div[contains(@id,'SIvCob')]//a[last()] .//div[contains(@id,'SIvCob')]//a[last()-1]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()]
This function is same as position() function where index number can be mentioned to get specific element.
.//div[contains(@id,'SIvCob')]//a[1]This function is used to find out immediate next elements of the component.
.//div[contains(@id,'SIvCob')]//a[contains(text(),'मराठी')]//following::aThis function is used to find out immediate preceding elements of the component.
.//div[contains(@id,'SIvCob')]//a[contains(text(),'मराठी')]//preceding::aIt 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::aIn above example, we are trying to find out all the list of element after ‘Gujarat’ which are sibling to these element.
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::aThis function is used to find immediate following element within the selected tag.
.//div[contains(@class,'Fgvgjc')]//child::styleThis function can be used to get the parent tag.
.//div[contains(@class,'Fgvgjc')]//parent::body .//div[contains(@class,'Fgvgjc')]//..This function is use to select all parent nod from the top.
.//div[contains(@id,'SIvCob')]//ancestor::divThis function is used to get child and grandchild of following nod.
.//div[contains(@class,'L3eUgb')]//descendant::aThis function is used to execute adding xpath query to get the multiple result.
.//div[contains(@class,'Ne6nSd')] | //div[contains(@class,'LLD4me')]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')]]
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(+) 5.2 Minus(-) 5.3 Multiplication(*) 5.4 Div 5.5 Equal(=) 5.6 Not Equal(!=) 5.7 Less than(<) 5.8 Less than equalTo(<=) 5.9 Greater Than(>) 5.10 Greater Than equalTo(>=) 5.11 Or 5.12 AND 5.13 MOD
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]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]Multiplication operator is used to multiply two numbers. Xpath example given below.
.//div[@id='SIvCob']//a[position()>=2*2]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]"=" 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()='हिन्दी')]"!=" Not equal to operator is used to exclude any specific element. In below xpath example, we exclude hindi language.
.//div[@id='SIvCob']//a[(text()!='हिन्दी')]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]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]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]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]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(),'मराठी')]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.
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() 6.2 concat() 6.3 start-with() 6.4 contains() 6.5 substring(string, number1, number2) 6.6 substring-before 6.7 substring-after 6.8 string-length 6.9 normalize-space
String function is used to covert any type of datatype in string. Refer above table for xpath
.//table[@id='xpath']//td[string(text())='20']Concat function is used to concate two attribute value togather and find out the result.
.//input[concat(@class,@value)='gNO89bGoogle Search']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']substring-before is used to find out the text before any specific text or symbol.
.//input[substring-before(@value,' ')='Google']substring-after is used to find out the text after any specific text or symbol.
.//input[substring-after(@value,' ')='Search']string length is used to find out the text of any specific length.
.//input[(@value=string-length('Google Search')>12)]
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 7.2 floor 7.3 celling 7.4 round 7.5 count
This will convert anytype in number. Refer above table for xpath
.//table[@id='xpath']//td[number(text())='10.11']
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']
It will return the nearest smallest integer value. Refer above table for xpath
.//table[@id='xpath']//td[ceiling(text())='10']
It will return the integer nearest in the value to number. Refer above table for xpath
.//table[@id='xpath']//td[round(text())='10']
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) 8.2 Not (Boolean) 8.3 True() 8.4 False()
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')]
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)]
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()]
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?
No comments:
Post a Comment