Locators
~30 mins
Locators are 8 types: (10min)
How to use Locators?
Basic Locators
-- id → can be identified as id attribute of an element
-- name →can be identified with name attribute
-- className → can be identified as class attribute
-- linkText → can be identified with text with
xpath
Xml path -> uniquely identify or address parts of an XML document
-> to navigate through a page's HTML structure and locate
the element
Syntax: driver.findElement(By.locator(“value"));
When to/ not to use?
| Locators | When do you use? | When you cannot use? |
|---|---|---|
| id | Always(Most preferred) | Numbers |
| name | If “id” does not exist | Duplicate |
| className | When classis unique without white spaces | White spaces, Duplicate |
| linkText | If it is a link | Duplicate |
| tagNname | For collection of objects | Duplicate |
| Xpath | If none of the above, works | Xpath can change |
| cssSelector | Thisisthe last option you have! | CSS can change |