- In automation we need to perform some operations on Webelement.
- For any website application there are some common pages on which you found button,textbox,radiobutton,dropdown button etc.
- For performing the action the first thing you need to find the xpath for it.
What is xpath?
In every web-based applications the XPaths or locators are unique addresses for each and every web object. It can be used with selenium to perform operations on each object present in the webpage. In the web page each web-object has a primary unique XPath.
Xpath detailed explations.The use of xpath.
- Selenium actully uses xpath to identify elements.
- Webpage has varies of object(eg:images,textboxes,textfields,buttons,radiobutton,checkboxes etc.)
- Each and every object has a unique id/name/path called xpath.
- If developer change the color of button in a webpage later but xpath remain same.
- If you are willing to become an automation tester in IT industry,Learning xpath is makes the thing easy.
- When automate a website,have to identify the object of the page and every webpage according to testcase.
How to Find xpath
How we get xpath for Mozilla Firefox1.Download Firebug
But before inspecting you need to download Firepath tool.
2.Download Firepath
After installing FirePath you can start finding xpath of any webelement.
There are two type of xpath.
1.Absolute xpath
2.Relative xpath
Absolute path will start with root path (/) and Relative path will from current path (//)
Absolute xpath
Let me take an example of https://www.facebook.com/
Go to https://www.facebook.com/
Now press F12 or fn+F12
Tool inspector for Finding xpath
Absolute xpath:
If you need to find out Absolute xpath select this General absolute xpath.It will give you absolute xpth.![]() |
| Absolute xpath of email Textbox. |
Relative xpath:
If you need to find out relative xpath then uncheck the Generate absolute xpath.![]() |
| Absolute xpath of email textbox. |
Absolute xpath sometime fails because sometime developer change the div.So if you will use absolute xpath technically it work but if any node change then xpath will not work.
So you need to find out Relative xpath.
Differences in Absolute and Relative xpath
Absolute XPath starts with the root node or a forward slash (/).Advantage
It identifies the element very fast.
Disadvantage
If any other tag added in between, then this path will no longer works.
Suppose Absolute xpath is
html/body/div[1]/div[1]/div/div/div/div/div/div/form/table/tbody/tr[2]/td[1]/input
If any of the div add in this xpath then this xpath will fail.
html/body/div[1]/div[1]/div/div/div/div/div2/div/div/form/table/tbody/tr[2]/td[1]/input
Relative xpath
1 Starts with //
2.//input” matches all the paragraph elements starts with input
3. //input[@id='email'] is a relative xpath.
Some basic concept you should all know.
Go to this link
Click on inspector and take the curser to your username.This will give you an xpath.
Note:This firebug always give you an xpath which is xpath by id.
But in selenium sometimes id may change so you have to take a different attributes.
How to write xpath mannually.
Syntax for path
//*[@attribute='value']
//is used to find relative element
* is used for tagname..Tagname is always input for textbox,radiobutton,button.
Tagname is always available for any element.
Tagname is always written with this < bracket Example <input,<p<li
Input,p,li are the tag name.
xpath for username
<input id="username" type="text" maxlength="40" value="" name="username">
input is tag name
Attributes are id,type,maxlength,name
values are username,text,40,username.Values are written in orange color.
//*[@attribute='value']
You can write xpath as:
Your first prefernce should be find id of that element
//input[@id='username']
After that you can use any attribute
Xpath by name
//input[@name='username']
Sometime attributes are same so you all need to find out unique element for object.












0 comments:
Post a Comment