Before performing any
action on the fields which are present in webpage the selenium has to uniquely
identify those fields and that can be done with the help of locators.
There are 8 eight type of locators:
1)ID:
The most efficient and preferred way to locate an element
on the webpage is by ID.
This will be unique which can be easily identified.
They are the most safest and fastest type of locators and always be the first
choice. Unfortunately there are some cases where element doesn't have unique
id. In these cases we can choose other alternative locator.
2)Name
When there is no ID , the next worth locator can be
name.
<button name="Submit">
3)LinkText
Finding an element with LinkText is very simple.If
there are multiple links with same link text in such cases selenium will
perform action on first matching element.
Eg:<a
href="Seleniumhq.org">Downloads</a>
4)Partial LinkText
Both LinkText and Partial LinkText can be used
only if the webelement which has html tag "a".
5)TagName
Tagname can be used with group elements like select,
checkboxes/dropdowns
6)ClassName
Finding an element with ClassName is also very simple
7)Xpath
xpath is path of element in Html tree. Inorder to
derive the xpath expression we should have complete knowledge on HTML tree.The
first forward slash (/) represents beginning of HTML tree which is called
root.(Absolute xpath)All other forward slash represents immediate
child(Relative xpath). Inorder to check whether xpath expression is correct or
not we can use firepath addon which is an additional tab in the firebug window.
8)CSS Selector
CSS stands for Cascade Style Sheet. It is a type of
locator with the following syntax:
Here we cant use ID, name or other locator, but we can
use TagName locator but it will be duplicate. In such cases we can use CSS
locator.
driver.findElement(By.cssselector("input[type='text']").sendkeys("xyz"));
driver.findElement(By.cssselector("input[type='password']").sendkeys("xyz"))