Site icon

Selenium Find Element using By ID Attribute


In this tutorial, we will learn to inspect and find element on a web page using the ID Attribute. We will test the TestLink Login page in this example. TestLink is an open-source Testcase Management application.

Environment and Tools

The environment and the tools used in this example:

Inspect Elements

To inspect elements using Firefox Web Developer Tools, follow this link:

Sample Test Example

In this example test we will identify the elements using the ID attribute. Launch the Web developer tools to inspect the elements in the login form.

For example the login name text box element using the id: tl_login“tl_login”)

The HTML markup code for the element is:

<input maxlength=”100″ name=”tl_login” id=”tl_login” type=”text” class=”form__input” placeholder=”Login Name” required=””>


Similarly repeat the process for the password textbox and Log in button.

The HTML code for the password text box is:

<input name=”tl_password” id=”tl_password” type=”password” class=”form__input” placeholder=”Password” required=””>

The id of the element is: tl_password

To identify the element we can use he following code:

driver.findElement(“tl_password”)) .sendKeys(“admin”);

Code Listing

package com.testingdocs.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import java.util.concurrent.TimeUnit;

// Selenium Tutorials -

public class FindByIDExampleTest {
	public WebDriver driver;

	public void setUp() throws MalformedURLException {
		DesiredCapabilities dCaps = new DesiredCapabilities();
		driver = new RemoteWebDriver(new 
URL("http://localhost:4444/wd/hub"), dCaps);

	public void testIDLocator() throws InterruptedException {
		try {
.implicitlyWait(60, TimeUnit.SECONDS);
			System.out.println(" demo Login 
with default credentials...");
		} catch (Exception e) {

	public void tearDown() {
		if (driver != null) {



Run the test with TestNG framework. Right click >> Run as >> TestNG Test

Note that we are just identifying elements and performing some actions on them. We are not testing any expected output in the test.

Selenium Tutorials on this website:

Official Website:

Exit mobile version