• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Selenium » Free Selenium Tutorial » ID Locators In Selenium WebDriver With Examples

ID Locators In Selenium WebDriver With Examples

Last Updated on: August 18, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Selenium ID Locators

In our previous post, we have discussed the different locators of selenium, and in this post, we are going to discuss “How we can locate Element By ID locator”.

Selenium ID Locators

ID locators are one of the most popular and famous ways to identify the elements in the web page. As per the W3C standard, every element supposes to be unique, which makes this ID locators one of the reliable locators.

ID locator is considered as one of the safest and fastest locator options and for every automation testers its the first choice when there are multiple-choice to locate the web elements.

Let’s take an example to understand how the ID locator work

Scenario:

  • Launch Mozilla Firefox
  • Open OrangeHRM On Firefox Browser
  • Locate the Enter User Name & Password Text box and enter a username & password Id with using the ID locator.
OrangeHRM
OrangeHRM

Code Snippet:

package com.selenium.practice.locator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class IdLocatorExample 
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.gecko.driver","/Software_Testingo_Practice/Selenium_Practice/Selenium_Practice_PGM/Browser_Driver/geckodriver.exe");

      driver=new FirefoxDriver();
      driver.get("https://orangehrm-demo-6x.orangehrmlive.com/auth/login");
      Thread.sleep(10000);
      
      driver.findElement(By.id("txtUsername")).clear();
      driver.findElement(By.id("txtPassword")).clear();

      driver.findElement(By.id("txtUsername")).sendKeys("Admin");
      driver.findElement(By.name("txtPassword")).sendKeys("admin");
      driver.findElement(By.id("btnLogin")).click();
   }
}

If you run the above Java program, then you will find out that OrangeHRM will be launched in the Mozilla Firefox browser, after that we are clear the existing value in the field Username and Password and after that, we are entering the username and password in the text box. I hope this gives you a clear understanding of how Id locator in Selenium works.

But since browsers do not make this thing mandatory that for each element the ID should be unique, developers take advantage of this that’s why for some of the elements in the web page ID is not present as a part of an attribute or due to auto-generated some elements have the same ID. Due to the mentioned issues, wen needs to use the other locators.

    Filed Under: Free Selenium Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers