Skip to main content

Selenium uses locators to find and match the elements on website page. Locator is a command that tells Selenium which GUI elements ( say Text Box, Buttons, Check Boxes etc) its needs to operate on. 

In selenium, with the help of locators we can click on buttons, can enter text in text boxes and can perform other operations on elements.

Locators are basically building blocks of the web page. Identification of correct GUI elements is a prior condition to creating an automation script. As per Selenium official website there are 8 locators:

Here is the list of Locators:

1. ID: Locates elements whose ID attribute matches the search value. Based on the World Wide Web Consortium (W3C) ID’s should be unique to elements and hence are considered as the fastest and safest method to locate element.

<input class="account_input" type="text" id="email">

In above example id of an element is email

2. Name: Locates elements whose NAME attribute matches the search value. An element can be defined via multiple attributes, one such attribute is Name. Name may or may not be unique on a page. In case there are elements with the same name, then the locator selects the first element with that name on the page.

<input class="account_input" type="text" name="email">

In above example name of an element is email

3. Link Text: Elements can be located via link text as in hyperlinks. In a scenario where there are multiple links of the same text, the first link would be selected. Link texts are prefixed with the anchor text and this locator can only be used for anchor tag.

<a class="login" href="#" rel="nofollow" title="">Sign in</a>

In above example link text is Sign in

4. Partial Link Text: Locating element via Partial Link Text works similar to normal Link Text locator. The reason of using the Partial Link Text locator in Selenium WebDriver over the Link Text Locator is only due to the reason when you have a long linktext and you intent to use only partial text to perform further actions on it. Sometimes the intent of using this can also be to locate multiple links on a page with a common partial text.

<a href="#" title="Contact Us">Contact us</a>

We can click on above element using partial link text Contact

5. Tag Name: As the name specifies, this css locator in Selenium WebDriver is used to identify elements with Tag names like div tag, span tag, a tag etc. A common example of this usage could be locating all links on your homepage and verifying whether they are functional or broken.

6. Class Name: Class Name locator helps in locating element defined through the class attribute. Below is an example of Lambdatest login DOM snapshot where login field is located via class name.

<input type="text" class="is_required validate account_input form-control" id="email_create" >

You can select element using class name is_required validate account_input form-control

7. CSS Selector: Locates elements matching a CSS selector. The CSS is mainly used to provide style rules for the web pages and you can use it for identifying one or more elements in the web page. The CSS selector is always the best possible way to locate complex elements in the page. In css # we use for id and dot(.) we use for class.

<input class="search_query form-control ac_input" type="text" id="search_query_top">

One of the css selector of above element will be input#search_query_top which is same as input[id='search_query_top']

8. XPath: If one has failed to identify an element by ID, class or name, one would need to locate the element through its XML path. This process may also be implemented while reading an XML document. 

<input class="search_query form-control ac_input" type="text" id="search_query_top">

 One of the css xpath of above element will be //input[@id='search_query_top']

Video Tutorial



Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.