Locators in Selenium

Locators are the way to find or address web elements in html page. In Selenium there are 8 locators are available as below: 

 

Find different elements on a web page using Locators 

xPath

xpath required for finding the dynamic element and

traverse between various elements of the web page

id

To find the element by ID of the element

name

To find the element by name of the element

linkText To find the element by text of the link

cssSelector

CSS path also locates elements having no name, class or ID.

className

To find the element by Classname of the element

tagName

To find total number of links 

partialLinkText To find any element what is long text with link attach

See the Examples below in video tutorial for all the Locators:

Types of X-path

There are two types of XPath:

1) Absolute XPath 

2) Relative XPath 

Absolute XPath :

It is the direct way to find the element, but the disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath gets failed. The key characteristic of XPath is that it begins with the single forward slash(/) ,which means you can select the element from the root node.

Below is the example of an absolute xpath expression of the element shown in the below screen.

Absolute xpath:

html/body/div[1]/section/div[1]/div/div/div/div[1]/div/div/div/div/div[3]/div[1]/div/h4[1]/b

Relative xpath:

For Relative Xpath is the path starts from the middle of the HTML DOM structure. It starts with the double forward slash (//), which means it can search the element anywhere at the webpage. You can start from the middle of the HTML DOM structure and no need to write long xpath.

Below is the example of a relative XPath expressions.

Relative xpath: //input[@id=’user-message’]

So, creating relative xpath locator using any attribute followed Syntax:
//tagName[@attribute=’value’]
OR: //* [@attribute=’value’]


cssSelector: There is no direct way to get CSS locator in Chrome unlike xpath. So we have to create cssSelector for any object following syntax:

tagName[attribute=’value’]
OR: *[attribute=’value’]

e.g:input[name=’email’]

– If there is id attribute and want to create Css locator, use # first then the value of the id. Syntax: #Login
— If there is class attribute and want to create Css locator, use . (dot) first then the value of the class. Syntax: .Password

Classes have spaces (ex: input r4 wide) shouldn’t use for locator as Compound classes cannot be accepted in Selenium. Same class may use multiple times.
Ex: Facebook email and password both objects have the same ClassName “inputtext”.
Doesnt work: driver.findElement(By.className(“btn-style class1 class2”)).click();


Notes: 

1) Every object may not have ID, ClassName OR Name-Xpath & CSS preferred.

2) Alpha numeric id may vary on every refresh-Check

3)Confirm the link object with anchor “a” tag in HTML

4)Classes have spaces (ex: input r4  wide) shouldn’t use for locator as Compound classes cannot be accepted in Selenium. Same class may use multiple times.

Ex: Facebook email and password both objects have the same ClassName “inputtext”.

5) Same values in different objects in same page- Selenium identify the 1st one as it scan’s from the top left.

6)Double quotes inside double quotes are not accepted in Java

7)Generate Xpath: Right click Copy on blue highlighted html code to generate xpath or use ChroPath tool or create xpath 

8)When xpath starts with html-Not reliable; Not recommended to use.


Share the Knowledge

You May Also Like

About the Author: codenbox

Leave a Reply

Your email address will not be published. Required fields are marked *