Open In App

Locating Strategies By ID Using Java

Last Updated : 26 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In Selenium WebDriver, locating a web element accurately is crucial for automating web applications. One of the most efficient ways to locate an element is by using its ID. The By.id strategy is often the most reliable and fastest locator, as the id attribute is usually unique within the HTML document.

Here are the steps of using the ID locator strategy in Selenium with Java.

Steps for Locating Strategies By ID Using Java

Step1: Create a Java Project In Eclipse IDE.

Launch Eclipse IDE and create a new project by go to File > New > Java Project.

Creating-new-Projet
Creating a new project

Step 2: Name The Java Project.

A new window will open prompting you to enter a Project Name. Provide a meaningful name that reflects the purpose or functionality of your project.

Locators_Demo
project name Locators_Demo

Step 3: Download Selenium and Configure Build Path.

A new project is created with the name Locators_Demo, the next step is to set up Selenium WebDriver. First, download the Selenium WebDriver for Java from the official Selenium website. After downloading, you need to configure Selenium JAR files in your IDE.

Link: Selenium Official Site.

Right-click on project--> Build Path -->Configure Build Path -->Class Path-->Add External Jars -->Select All Jars-->Apply And Close.

Configure-Build-Path
Configure Build Path

Go to libraries and Select Classpath then click on the Add the External JARs...

Add-External-Jars-1
Add External Jars

Add the required libraries from your local files in your computer.

Add-External-Jars-2
Add External Jars

After adding libraries (Jars) click on the Apply or Apply and Close Button.

Add-External-Jars-3
Add External Jars

Step 4: Download Chrome Driver

Right-click on Project to create a new folder to store the Chrome driver.

Download a compatible version of Chrome driver for your Chrome browser then extract the files into the Chrome driver folder.

Chrome Driver Download Link: Google Chrome Drivers.
chromedriver-folder
Create a folder

Step 5: Create a New Class File

Right-click on the src folder then create a package name as com.selenium. Test under this package create one class name as ByIdDemo the click on finish. Then the project folder structure is shown as the below pictures.

create-a-class-(1)
Create Class

Name the class as ByIdDemo and press the Enter or click on the Finish button.

ByIdDemo
ByIdDemo.java

Step 6: Selenium WebDriver Coding

Then update the class file code as bellow and replace the path of Chrome-Driver from your local files.

Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;

public class ByIdDemo {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "./ChromeDriver/chromedriver-win32/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.facebook.com/");
        driver.findElement(By.id("email")).sendKeys("[email protected]");
        driver.findElement(By.id("pass")).sendKeys("12345");
        driver.findElement(By.name("login")).click();
    }
}

Step 7: Run The Java Project

Right-click on the class then select Run as Java Application.

Run-As-Java-Application
Run As Java Application

Output : Face Facebook login page will automated by the opening typing email and password and then clicking on the login button.

Screenshot-(36)

Conclusion

In conclusion, the ID locator is a unique and efficient way to identify an element on a web page. Located under the By class in Selenium, the ID is typically the most reliable and fast method for locating elements because each ID is guaranteed to be unique within the page's DOM (Document Object Model). This uniqueness ensures that an ID can precisely target a specific element, making it an essential locator for automation tasks in Selenium.


Article Tags :
Practice Tags :

Similar Reads