In last article we saw various web elements, to perform any action on any element. We need to find or locate those element first within webpage .
We should be able to identify element uniquely to perform any actions. Different locators we will be using are as follows.
Locators:
- ID
- Name
- Class Name
- Link text
- Partial Link text
- CSS
- XPath
Where I will find above locators to locate web element?
You will find many of the above locators in HTML code of the page also called as DOM (Document Object Model).
How to see DOM?
Go to browser, Open the site and just press key “F12“, you will see the DOM of the current page.
Which locator should I use?
You should use locator in the order mentioned above. If you are able to find element using “Id”, you have better luck, if not then go for Name and So on. Last one is XPath. Don’t forget that you need to fine element uniquely.
Why I should follow this order?
Order is important, it has relation with performance of script execution. For XPath performance is poor as WebDriver has to scan whole DOM to find element using XPath.
Click on each of the below locator to see, how it works in Selenium.