Making The Move With ID Locator In Selenium WebDriver

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

42380 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.

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.

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.

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(By.id("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:

Check-in:

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! 🙂

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 qavibes.blogspot.com 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

Cross Browser Testing Is Not Only About Browsers

Cross Browser Testing Is Not Only About Browsers

64734 Views | 11 Min Min Read

Leave a Reply to misha kapoor Cancel reply

Your email address will not be published. Required fields are marked *