Making The Move With ID Locator In Selenium WebDriver

Sadhvi Singh

Posted On: April 15, 2019

view count69985 Views

Read time4 Min Read

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Locators Tutorial.

If you are beginning with Selenium, you may be unsure about what to do and how to do it? In my opinion, I find locators to be the best step to start brushing or learning Selenium. Locators are the foundation of building your Selenium Script. With the use of locators, one can locate the element on the web page. Not just locating an element is important but making sure it’s fast and accurate is equally important. One such locator is the ID locator in Selenium WebDriver that does this business.

Check out what WebDriver is, its features, how it works, best practices, and more in this WebDriver tutorial.

When To Opt For ID Locator In Selenium WebDriver?

Using ID Locator in Selenium WebDriver is the fastest and the most reliable among all the locators. ID’s are supposed to be unique to each element, making the ID locator as a dependable choice. Since browsers do not make it mandatory for ID to be unique thereby making developers take leverage of it and may lead to ID’s either not present as part of an attribute or autogenerated, or not unique to a page. Due to the mentioned issues, it may require switching to other locators.

You can check out other articles around different CSS locator in Selenium that helps in locating elements through various ways:

If you are an advanced or medium Selenium practitioner, then you can chuck on dedicated articles mentioned above. And go for our complete guide to help you illustrate the practical demonstration of CSS locator in Selenium. If you’re looking to improve your Selenium interview skills, check out our curated list of Selenium interview questions and answers.

Check Out My Complete Guide For Every CSS Locator In Selenium WebDriver With Examples

Using ID Locator in Selenium WebDriver

Here I will be sighting an example of LambdaTest login page example for ‘Remember me’ functionality. Below is the screenshot of the same.

Using ID Locator in Selenium WebDriver

Referencing the below DOM structure for ‘Remember me’ element:

As you can see, the above tag has multiple attributes, one of which is the ID locator.

This certification is for anyone who wants to stay ahead among professionals who are growing their career in Selenium automation testing.

Here’s a short glimpse of the Selenium 101 certification from LambdaTest:

Syntax For ID Locator In Selenium WebDriver

In order to use the ID Selenium locator to find an element via ID we use the below syntax:

driver.findElement("remember "));

Now, let’s try to incorporate this into a real-time example. In this example, I will be using the facebook login page, to login using ID locators for interacting with the WebElement object.

Below is the DOM structure for email, password and log in field of the facebook login page:

Referenced is the screenshot highlighting the same:

Syntax For ID Locator In Selenium WebDriver


Delving Into Code For ID Locator In Selenium WebDriver

The below code will help you relate with the demonstration of ID locator in Selenium WebDriver in a better way. Let’s dig into it:

Another Example For Demonstrating The ID Locator

Let’s cater to another similar example for ID where we are making a booking through Airbnb:

Another Example For Demonstrating The ID Locator

Below is the DOM structure for the same:

Location field:


Search Button:

Now let’s dig into the code snippet:

It’s A Wrap!

ID’s are the most reliable and easiest way to locate an element owing to its uniqueness. So if you are starting with Selenium WebDriver, ID’s can be your best friend. But make sure you to hit on the right track while locating them, and be aware of duplicate IDs or auto-generated ones. Happy testing! 🙂

Author Profile Author Profile Author Profile

Author’s Profile

Sadhvi Singh

Sadhvi Singh is a QA Manager. In 7 years of her professional journey, she has worked on multiple domains and testing techniques like Automation testing, Database testing, API testing, Manual testing, and Security testing. With multiple tools exposure added to her experience, she has skilled herself further through her two major accolades at International level through ISTQB with foundation and advanced levels.

Blogs: 10