Locators in Selenium WebDriver | Selenium Tutorial

Locators in Selenium – Before going ahead, I assume you have already gone through the installation of FireBug and FirePath. If not, check out the link mentioned below:

How To Install Fire Bug, Fire Path

We use FireBug and FirePath to identify the Locators in Selenium

Hope you have already installed Selenium WebDriver. If not go through the below mentioned link to download and install Selenium WebDriver.

How To Download And Install Selenium WebDriver

Selenium identifies the elements to be worked on using the following locators.

Different types of Locators in Selenium are as follows:

i. ID
ii. Name
iii. Class Name
iv. Tag Name
v. Link Text & Partial Link Text
vi. CSS Selector
vii. XPath

Locating elements in WebDriver is done by using the method “findElement(By.locator())“.

We use above mentioned method to locate elements but the locator() part will be replaced with the locator names. Mentioned few examples below for reference.

Here is the link to the post “Difference between findElement and findElements methods

Let’s see each of the Locators in Selenium in detail:

ID Locator:

ID’s are unique for each element so it is common way to locate elements using ID Locator. As per W3C, ID’s are supposed to be unique on a page and it makes ID’s are the most reliable locator. ID locators are the fastest and safest locators out of all locators.

id = id of the element

Click this link for detailed explanation of ID Locator

Name Locator:

We sometimes use Name locator to identify the elements on our webpage. Locating elements using Name is same as locating elements using ID locator.

These are not unique on a page. If there are multiple elements with the same Name locator then the first element on the page is selected. Test may fail, if another element with the same Name locator is present on the web page or added by the developers in the later stages.

Name = Name of the element

Click this link for detailed explanation of Name Locator

Class Name Locator:

Class Name locator gives the element which matches the values specified in the attribute name “class”.

Click this link for detailed explanation of Class Name Locator

Tag Name Locator:

Tag Name locator is used to find the elements matching the specified Tag Name. It is very helpful when we want to extract the content within a Tag.

Click this link for detailed explanation of Tag Name Locator

Link Text Locator:

If there are multiple elements with the same link text then the first one will be selected. This Link Text locator works only on links (hyperlinks) so it is called as Link Text locator.

Click this link for detailed explanation of Link Text Locator

Partial Link Text:

In some situations, we may need to find links by a portion of the text in a Link Text element. it contains. In such situations, we use Partial Link Text to locate elements.

Click this link for detailed explanation of Partial Link Text Locator

CSS Selector Locator:

There is a debate on the performance between CSS Locator and XPath locator and the debate on the performance of CSS and XPath locator is out of scope of this post. Most of the automation testers believe that using CSS selector makes the execution of script faster compared to XPath locator. This locator is always the best way to locate elements on the page.

Following are the some of the mainly used formats of CSS Selectors.

  • Tag and ID
  • Tag and Class
  • Tag and Attribute
  • Tag, Class and Attribute

Click this link for detailed explanation of CSS Selector Locator

XPath Locator:

XPath is designed to allow the navigation of XML documents, with the purpose of selecting individual elements, attributes, or some other part of an XML document for specific processing. XPath produces reliable locators but in performance wise it is slower (especially in IE older versions) compared to CSS Selector.

Click this link for detailed explanation of XPath Locator

Sumasri is a Sr. Software Test Engineer. Currently working in an MNC. She is a co-founder of Software Testing Material. She has an exclusive experience in the field of Software Testing. She writes here about Manual Testing and Automation Testing.

Leave a Comment

Share via
Copy link
Powered by Social Snap