Skip to main content
Home
  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact
programsbuzz facebook programsbuzz twitter programsbuzz linkedin
  • Log in

Main navigation

  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact

Locators in Selenium

Profile picture for user devraj
Written by devraj on 08/29/2020 - 23:02

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

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

Locators are building blocks of the web page. Identifying correct GUI elements is a prior condition to creating an automation script. As per Selenium's official website, there are eight traditional locators. 

Here is the list of Selenium Traditional Locators:

1. ID

Locates elements whose ID attribute matches the search value. IDs should be unique to all elements and are considered the quickest and safest way to find elements.

HTML

<input type="password" id="edit-pass" name="pass" class="form-text required">

In the above example, the id of an element is the edit-pass.

Selenium Java Code

driver.findElement(By.id("edit-pass")).sendKeys("Hello");

2. Name

Locates elements whose name attribute matches the search value. The name may or may not be unique on a page. If elements have identical names, the locator selects the first element with that name on the page.

HTML

<input type="password" id="edit-pass" name="pass" class="form-text required">

In the above example Name of an element is pass.

Selenium Java Code

driver.findElement(By.name("pass")).sendKeys("Hello");

3. Link Text

Elements can be located through link text present in hyperlinks. The first link will be selected if there is more than one link of the same text. Link texts is associated with the anchor tag, and this locator can only be used for the anchor tag.

HTML

<a href="/user/register" data-drupal-link-system-path="user/register">Create new account</a>

In the above example, the link text is Create new account

Selenium Java Code

driver.findElement(By.linkText("CREATE NEW ACCOUNT")).click();

4. Partial Link Text

Locating elements via Partial Link Text works similar to a regular Link Text locator. The motivation for using the Partial Link Text locator in Selenium WebDriver over the standard Link Text Locator is that when you have a long link text, you want to use only partial text to perform further actions on it. Sometimes, this technique can also be used to locate multiple links on a page with a common partial text.

HTML

<a href="/user/register" data-drupal-link-system-path="user/register">Create new account</a>

We can click on the above element using partial link text CREATE NEW.

Selenium Java

driver.findElement(By.partialLinkText("CREATE NEW")).click();

5. Tag Name

As the name specifies, this CSS locator in Selenium WebDriver identifies elements with Tag names like span tag, div tag, input tag or a tag, etc. A common use case is locating all links on your page and verifying whether they are broken or functional.

Selenium Java

driver.findElement(By.tagName("input")).sendKeys("hello world");

This will enter hello world value in first input element of the page. 

6. Class Name

Class Name locator helps locate elements defined through the class attribute. 

HTML

<input id="edit-mail" name="mail" class="form-email required">

You can select an element using the class name form-email or required. Classes are separated by space.

Selenium Java Code

driver.findElement(By.className("form-email")).sendKeys("hello@example.com");

7. CSS Selector

Locates elements matching a CSS selector. The CSS selector is always the best way to locate complex elements on the page. We use # for id and dot(.) for class in CSS.

HTML

<input type="text" id="edit-name" name="name" class="form-text required" required="required">

One of the css selector of above element will be #edit-name

Selenium Java Code

driver.findElement(By.cssSelector("#edit-name")).sendKeys("myusername");

8. XPath

If one has failed to identify an element by ID, class, Name or CSS, one would need to locate the element through its XML path. This process is same as reading an XML document. 

HTML

<input type="text" id="edit-name" name="name" class="form-text required" required="required">

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

Selenium Java Code

driver.findElement(By.xpath("//input[@id='edit-name']")).sendKeys("myusername");

That's all about Traditional Locators, Selenium has introduced Relative Locator in version 4 which we will discuss in next article. 

Related Content
Selenium Tutorial
Difference between Selenium and Cucumber
Page Object Model with PageFactory in Selenium
Tags
Selenium
  • Log in or register to post comments

Choose Your Technology

  1. Agile
  2. Apache Groovy
  3. Apache Hadoop
  4. Apache HBase
  5. Apache Spark
  6. Appium
  7. AutoIt
  8. AWS
  9. Behat
  10. Cucumber Java
  11. Cypress
  12. DBMS
  13. Drupal
  14. GitHub
  15. GitLab
  16. GoLang
  17. Gradle
  18. HTML
  19. ISTQB Foundation
  20. Java
  21. JavaScript
  22. JMeter
  23. JUnit
  24. Karate
  25. Kotlin
  26. LoadRunner
  27. matplotlib
  28. MongoDB
  29. MS SQL Server
  30. MySQL
  31. Nightwatch JS
  32. PactumJS
  33. PHP
  34. Playwright
  35. Playwright Java
  36. Playwright Python
  37. Postman
  38. Project Management
  39. Protractor
  40. PyDev
  41. Python
  42. Python NumPy
  43. Python Pandas
  44. Python Seaborn
  45. R Language
  46. REST Assured
  47. Ruby
  48. Selenide
© Copyright By iVagus Services Pvt. Ltd. 2023. All Rights Reserved.

Footer

  • Cookie Policy
  • Privacy Policy
  • Terms of Use