Web Automation Using Selenium
  • About The Book
  • Selenium
    • Introduction
      • What is testing and why?
      • What is Selenium and Why?
    • Getting Started
      • Audience
      • Prerequisite
      • Set up
      • Getting Started - Hello World..!!
      • What are drivers
    • Locators
      • CSS Selectors
      • XPath Selector
    • WebDriver
      • WebDriver Methods
      • WebDriver Types
    • WebElement(s)
      • WebElement Methods
      • Looping Through WebElements
    • Waits In Selenium
  • TestNG
    • Introduction
    • TestNG Setup
    • First Test Script
    • Some of the features of TestNG
    • TestNG Annotations
      • @Test
      • @DataProvider
  • Maven
    • Introduction
  • Framework Development
    • Introduction
    • Building Framework
      • Technology Stack
      • Page Object Model (POM)
      • Setup
      • What our framework contain
      • Packages
        • Context
        • Factory
        • Listeners
        • Pages
      • Creating first automation test
  • Common Interview Questions
    • Selenium-Related
Powered by GitBook
On this page
  • Id:
  • Name:
  • Tag name:
  • Link text:
  • Partial link text:

Was this helpful?

  1. Selenium

Locators

Mechanism to find elements in a web page

There are 7 ways which we can use to find an element or group of elements in a web page. And they are

  1. Id

  2. Name

  3. Tag name

  4. Link text

  5. Partial link text

  6. CSS Selector

  7. XPath

Id:

If an element has id attribute then we can use it find the element.

WebElement element = driver.findElement(By.id("id-value"));

Name:

If an element has name attribute then we can use it find the element.

WebElement element = driver.findElement(By.name("name-value"));

Tag name:

We can find an element using it'stagname .

WebElement element = driver.findElement(By.tagName("a"));

Link text:

If an element is having tag a , then we can identify it using the link text of it.

WebElement element = driver.findElement(By.linkText("link-text-value"));
// For example in Goole search page we can find the gmail link element as
WebElement element = driver.findElement(By.linkText("Gmail"));

Partial link text:

If an element is having tag a , then we can identify it using a portion of it's text.

WebElement element = driver.findElement(
                     By.partialLinkText("partial-value-of-link-text"));
// For example in Goole search page we can find the gmail link element as
WebElement element = driver.findElement(By.partialLinkText("mail"));

All the above mentioned locator mechanisms are straight forward.

But what if the element doesn't have any unique id, name or link text

Then the last two locators mechanisms help to locate the elements.

We'll be seeing them in the separate sections.

PreviousWhat are driversNextCSS Selectors

Last updated 4 years ago

Was this helpful?