Introduction
The ID Locator is one of the most commonly used and reliable locator strategies in Selenium WebDriver. It locates a web element using the value of its id attribute in the HTML document.
According to HTML standards, an ID should be unique within a webpage. Because of this uniqueness, Selenium can quickly and accurately identify elements using their IDs, making the By.ID locator the preferred choice whenever it is available.
The ID locator is widely used for locating text boxes, buttons, checkboxes, radio buttons, dropdowns, and other web elements in automation scripts.
In this tutorial, you will learn what the ID locator is, why it is used, its syntax, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.
What is an ID Locator?
The ID Locator is a Selenium locator strategy that identifies a web element using the value of its HTML id attribute.
For example, consider the following HTML element:
<input id="username" type="text">
The element can be located using:
driver.find_element(By.ID, "username")
Since IDs are generally unique, Selenium returns the correct element quickly and reliably.
Why Do We Use the ID Locator?
The ID locator is commonly used because it:
Is usually unique on a webpage.
Is the fastest locator strategy.
Makes automation scripts easy to read.
Reduces maintenance effort.
Improves automation stability.
Is recommended as the first choice whenever available.
Syntax
driver.find_element(By.ID, "id_value")
Where:
By.ID specifies that Selenium should locate the element using its ID.
id_value is the value of the HTML
idattribute.
Practical Example
The following example demonstrates how to locate the username input field using its ID.
The automation script opens the Selenium practice login page, locates the username field using its unique ID, and verifies that the correct element has been found.
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 11. Basic Locators - ID
# Practice site: https://the-internet.herokuapp.com/login
#
# By.ID locates an element using its id attribute value.
def test_find_element_by_id():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/login")
username = driver.find_element(By.ID, "username")
assert username.get_attribute("id") == "username"
finally:
driver.quit()
Output
Browser launched successfully.
Login page opened successfully.
Username field located using ID.
Element ID:
username
Assertion Passed.
Test Executed Successfully.
Understanding the Code
Import Required Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
Imports the Selenium WebDriver module and the By class required for locating web elements.
Launch Chrome Browser
driver = webdriver.Chrome()
Starts a new Chrome browser session.
Open the Login Page
driver.get("https://the-internet.herokuapp.com/login")
Navigates to the Selenium practice login page.
Locate the Username Field Using ID
username = driver.find_element(By.ID, "username")
Locates the username input field using its unique HTML id attribute.
Validate the Located Element
assert username.get_attribute("id") == "username"
Verifies that the located element has the expected ID.
If the assertion passes, Selenium has successfully located the correct element.
Close the Browser
driver.quit()
Closes all browser windows and ends the WebDriver session.
Execution Flow
Launch Browser
│
▼
Open Login Page
│
▼
Locate Element Using ID
│
▼
Verify Element ID
│
▼
Assertion Passes
│
▼
Close Browser
Automation Testing Example
Suppose you’re automating the login page of an HR management system.
The username field has the following HTML:
<input id="username">
The element can be located easily using:
driver.find_element(By.ID, "username")
This is the recommended approach because the ID uniquely identifies the element.
Real-World Example
Consider an online banking application where the Login button has the following HTML:
<button id="loginBtn">
Login
</button>
You can locate the button using:
driver.find_element(By.ID, "loginBtn")
This makes the automation script simple, readable, and reliable.
Common Mistakes Beginners Make
Using the Wrong ID Value
Incorrect
driver.find_element(By.ID, "user")
If the actual ID is "username", Selenium throws a NoSuchElementException.
Correct
driver.find_element(By.ID, "username")
Confusing ID with Name
Incorrect
driver.find_element(By.ID, "password")
when the element only has:
<input name="password">
Always inspect the HTML carefully before selecting a locator.
Assuming Every Element Has an ID
Not every HTML element contains an ID attribute.
If an ID is unavailable, choose another reliable locator such as Name or CSS Selector.
Best Practices
Always prefer ID whenever it is available.
Verify that the ID is unique.
Avoid using dynamically generated IDs if they change on every page load.
Keep locator names meaningful and readable.
Use browser Developer Tools to inspect the ID before writing automation scripts.
Conclusion
The ID Locator is the most reliable and preferred locator strategy in Selenium. Since IDs are generally unique, they allow Selenium to locate web elements quickly and accurately.
Whenever an element has a stable and unique ID, it should be your first choice for writing Selenium automation scripts.
Mastering the ID locator is one of the first and most important steps toward building stable and maintainable Selenium automation frameworks.
Frequently Asked Questions (FAQs)
What is the ID Locator in Selenium?
The ID locator identifies a web element using the value of its HTML id attribute.
What is the syntax for the ID locator?
driver.find_element(By.ID, "id_value")
Why is the ID locator preferred?
Because IDs are usually unique, making them the fastest and most reliable locator strategy.
Can multiple elements have the same ID?
According to HTML standards, IDs should be unique. If multiple elements share the same ID, the webpage is incorrectly designed.
What should I do if an element does not have an ID?
Use another locator such as Name, CSS Selector, or XPath.
Key Takeaways
The ID Locator identifies elements using their HTML
idattribute.The syntax is
driver.find_element(By.ID, "id_value").IDs are generally unique, making this the most reliable locator strategy.
Always prefer the ID locator whenever it is available.
Verify that the ID is stable and not dynamically generated.
If no ID exists, choose the next best locator such as Name or CSS Selector.
The ID locator is one of the most commonly asked Selenium interview topics and an essential automation skill.
