Locators in Selenium – Locate by ID, ClassName, Name, TagName, LinkText, PartialLinkText

HOME

In this tutorial, we will use http://www.facebook.com for showing the use of different locators

How to find web elements?

1)  Locating by ID

Find the input element with ID.

Syntax

driver.findElement(By.id(“email”))

 

2)  Locating by ClassName

 Find the input element with name of class

Syntax 

driver.findElement(By.ClassName("inputtext"))

3)  Locating by TagName

Find the input element with TagName

Syntax 

driver.findElement(By.tagName("select"))

4)  Locating by Name

Find the input element with matching name attribute.

Syntax 

driver.findElement(By.name("email"))

5)  Locating by Linktext

Find the link element with matching visible text

Syntax

driver.findElement(By.linkText("Forgot account?"))

Find the link element with partial matching visible text

Syntax

driver.findElement(By.partialLinkText("Forgot "))

7)  XPath

Full form of XPath is XML Path. It is used to locate a web element on a web page using HTML DOM structure 

Syntax

By.xpath("//tagname[@Attribute = ‘value’]")

//: select the current node.
tagname: name of the tag of a particular node.
@: to select attribute.
Attribute: name of the attribute of the node.
Value: value of the attribute

There are 2 types of Xpath:-

1) Absolute Path
2) Relative Path

Absolute Path – It is the direct way to find any element. But as name suggest, it needs to be absolute. Any change made in the path will fail the xpath, result tool will not be able to locate the we element on the page.

Syntax

driver.findElement(By.xpath("//*[@id='yDmH0d']/div/div/div[2]/div[2]/div/div/div/div/div/div/ form/content/section/div/content/div/div/div/div/div/input"))

Relative Path –Relative XPath begins from the current location and is prefixed with a “//”. It starts from the middle of the DOM structure. To know more about XPath, click here

Syntax

By.xpath("//input[@name='identifier']")

8)  CSS Selectors 

CSS Selectors are string patterns used to identify an element based on a combination of HTML tag, id, class, and attributes. Locating by CSS Selector is more complicated than the previous methods, but it is the most common locating strategy of advanced Selenium users because it can access even those elements that have no ID or name. To know more about CSS, click here

Below is an example that shows how to use different locators. In this example, we have used Id, name, className, tagName, partialLinkText and LinkText locators.

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.time.Duration;
import java.util.List;

public class Automation_Practice_Form {

    protected static WebDriver driver;
    public static void main(String[] args) {

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
        driver.get("https://opensource-demo.orangehrmlive.com/");
        driver.manage().window().maximize();

        // Locate link by partialLinkText
        String PartialLinkText_Text = driver.findElement(By.partialLinkText("Orange")).getText();
        System.out.println("Text of PartialLinkText Locator is ->" + PartialLinkText_Text);

        // Locate FirstName by name
        driver.findElement(By.name("username")).sendKeys("Admin");

        // Locate LastName by id
        driver.findElement(By.name("password")).sendKeys("admin123");

        // Locate options of DropDown by tagName
        List<WebElement> linkName = driver.findElements(By.tagName("a"));
        System.out.println("List of Links:");
        for (WebElement link : linkName) {
            System.out.println(link.getText() + " - " +link.getAttribute("href"));
        }
        driver.close();
    }
}

7 thoughts on “Locators in Selenium – Locate by ID, ClassName, Name, TagName, LinkText, PartialLinkText

  1. Hi Vibha, your blogs are very informative. I have read all your blogs. Apart from Internet Explorer, Google Chrome and Firefox, selenium use any other driver too?

    Like

  2. Thanks Nishant. Yes, apart from IE, Chrome and Firefox, there are other drivers too in Selenium.SafariDriverOperaDriverAndroidDriverIPhoneDriverHtmlUnitDriverMicrosoftEdgeDriverGhostDriver -WebDriver Wire Protocol for PhantomJSios-driverSelendroid – For mobile TestingBlackberry 10QtWebDriver – WebDriver implementation for QtjBrowserDriver – Web browser driver compatible with the Selenium WebDriver spec – headless, WebKit-based, pure Java

    Like

  3. I have updated my blog for the syntax of Absolute Path. It looks like thisdriver.findElement(By.xpath(\”//*[@id='yDmH0d']/div/div/div[2]/div[2]/div/div/div/div/div/div/ form/content/section/div/content/div/div/div/div/div/input\”)

    Like

Leave a comment