Locating Strategies By ID Using Java
Last Updated :
26 Jun, 2025
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 a new projectStep 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.
project name Locators_DemoA 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 PathGo to libraries and Select Classpath then click on the Add the External JARs...
Add External JarsAdd the required libraries from your local files in your computer.
Add External JarsAfter adding libraries (Jars) click on the Apply or Apply and Close Button.
Add External JarsStep 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.
Create a folderStep 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 ClassName the class as ByIdDemo and press the Enter or click on the Finish button.
ByIdDemo.javaStep 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 ApplicationOutput : Face Facebook login page will automated by the opening typing email and password and then clicking on the login button.
.png)
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.
Similar Reads
Locating Strategies By XPath Using Java Web application testing must be rigorous and thorough. For this reason, many tests for web applications are automated. Selenium is an open-source framework that allows us to automate web browser testing. In the following article, we will explore how to use XPath to locate elements on our HTML code u
4 min read
Locating Strategies By Class Name Using Java Efficiently locating elements by their Class Name is important for successful automation testing in Java. In this article, we explore the use of Selenium's By.className() method to locate web elements, providing step-by-step guidance on setting up a Java project, adding Selenium JAR files, and demon
4 min read
Locating Strategies By Tag Name Using Java Web application testing must be rigorous and thorough. For this reason, many tests for web applications are automated. Selenium is an open-source framework that allows us to automate web browser testing. Table of Content Steps for Locating Elements By Tag Name in JavaConclusionFAQ's on Locating Stra
6 min read
Locating Strategies By Partial Link Text Using Java Web application testing must be rigorous and thorough. For this reason, many tests for web applications are automated. Selenium is an open-source framework that allows us to automate web browser testing.In the following sections, we will use partial link text to locate elements on our HTML code usin
4 min read
Selenium Locating Strategies Selenium is one of the most powerful and widely used tools for automated web applications. Whether you're a software developer or a QA tester selenium is an important tool to have in your toolkit. One of the most important tasks in order to automate the web applications is interacting with the eleme
11 min read
Java String indexOf() Method In Java, the String indexOf() method returns the position of the first occurrence of the specified character or string in a specified string. In this article, we will learn various ways to use indexOf() in Java.Example:Below is the simplest way that returns the index of the first occurrence of a spe
3 min read