Making The Move With ID Locator In Selenium WebDriver

Posted by Sadhvi Singh | April 15, 2019
Automation • Selenium Locators •

30688 Views | 4 Min Read

Making The Move With ID Locator In Selenium WebDriver

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.

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.

Other than ID locator in Selenium WebDriver, there are multiple locators in Selenium WebDriver:

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:

< input type="checkbox" name="remember" id="remember" class="form-check-input" >

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

Syntax For ID Locator In Selenium WebDriver

In order to use the ID locator in Selenium WebDriver 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:

< input type="email" class="inputtext" name="email" id="email" value="" data-testid="royal_email" >

< input type="password" class="inputtext" name="pass" id="pass" data-testid="royal_pass" >

< input value="Log In" aria-label="Log In" data-testid="royal_login_button" type="submit" id="u_0_8" >

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:

< input type="text" class="_up0kwni" aria-autocomplete="list" aria-describedby="Koan-magic-carpet-koan-search-bar__description" aria-expanded="false" autocomplete="off" autocorrect="off" spellcheck="false" id="Koan-magic-carpet-koan-search-bar__input" name="query" placeholder="Anywhere" role="combobox" value="Delhi" >


< input type="text" class="_14fdu48d" data-veloute="checkin_input" id="checkin_input" name="checkin" placeholder="dd-mm-yyyy" value="Wed, 10th Apr" readonly="" >


< input type="text" class="_14fdu48d" data-veloute="checkout_input" id="checkout_input" name="checkout" placeholder="dd-mm-yyyy" value="Sun, 14th Apr" readonly="" >

Search Button:

< button type="submit" class="_z5x9aua" aria-busy="false" >< span class="_ftj2sg4" >Search< /span >< /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! 🙂

Written by 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. She has a technical blog named as where she caters to all the challenges offered in the day to day journey of the quality assurance aspirants. In her spare time, she also works as a technical trainer and mentor to many budding QA professionals.

Related Articles