Saturday, July 17, 2021

A-Z About Selectors | What are the locator in selenium??

In simple Term locator is some unique attribute which we can use to identify the Web Element on a web page in order to perform an action on it.

Any Web Element on a page is created using HTML attribute ( name , type , id , etc ) . we can use their attribute to perform an action on it.



Image by: https://www.besanttechnologies.com/



1) Id locator:

 ID is the most common locator used in selenium. Syntax to locate web element using ID is

For example, we can locate 'signup' button of facebook signup page as bellow



driver.findElement(By.id("u_3_s_1I");


2) Name:
We can locate a web element using Name attribute also. syntax for locating web element using name is 




driver.findElement(By.name("firstname");


3)Class name : To locate a web element using class name





driver.findElement(By.className("inputtext _58mg _5dba _2ph-");

4) TAG name :



driver.findElement(By.tagName("input");

5) Link Text , Partial Link Text : 



driver.findElement(By.linkText("terms"));

The only difference between link text and partial link text is if we use partial link text it will find link which contains a keyword while link text will find the link which contain the excat keyword. so if we have a link for which text is changing by some part of text is not changing we can use the partial link text.

driver.findElement(By.partialLinkText("terms"));

6) CSS locator : CSS locator is fastest locator in selenium as compare to any other locator



input[name="firstname"]

7) X-path locator :


//button[@type='submit']
X-Path and CSS locator are very broad topic , i wil create a another blog for it in which i will cover X-PATH axes.