How To Locate Elements By ID In Selenium WebDriver With Example

Before using WebDriver software testing tool, You must be aware about different ways of locating an elements in WebDriver for software web application. Locating an element is essential part in selenium WebDriver because when you wants to take some action on element (typing text or clicking on button of software web application), first you need to locate that specific element to perform action. Let’s learn how to locate element by ID.

First of all I recommend you to read all these selenium IDE element locating methods and then read this article about Selenium WebDriver element locators to get all locating methods in better way.
WebDriver software testing tool which is also known as a Selenium 2 has many different ways of locating element. Let me explain each of them with examples. Here I am explaining how to locate element By id and will describe Locating Web Element By ClassName in my Next Post and others in latter posts.
Locating UI Element By ID
If your software web application’s page element has unique and static ID then you can locate your page element by ID. Look in to bellow given image. You can verify that your webelement has any id or not using the firebug.

In above image, Submit Query button has unique id = ‘submitButton’. I can use that id to locate button as bellow.

driver.findElement(By.id(“submitButton”));

Bellow given example will show you how to locate element by id and then how to click on it. Copy bellow given @Test method part and replace it with the @Test method part of example given on this page.(Note : @Test method is marked with pink color in that example).

 @Test
 public void test() {
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  for (int i = 0; i<=20; i++)
  {
   WebElement  btn = driver.findElement(By.id("submitButton"));//Locating element by id
   if (btn.isEnabled()) 
   {
    //if webelement's attribute found enabled then this code will be executed.
    System.out.print("nCongr8s... Button is enabled and webdriver is clicking on it now");

   //Locating button by id and then clicking on it.
    driver.findElement(By.id("submitButton")).click(); 
    i=20;
    
   }
   else
   {
    //if webelement's attribute found disabled then this code will be executed.
    System.out.print("nSorry but Button is disabled right now..");
   }
   try {
    Thread.sleep(500);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }

17 thoughts on “How To Locate Elements By ID In Selenium WebDriver With Example

  1. Hi, i just started learning java and selenium so plse bear with me for my questions. Alright i wanted to ask what is the use of for loop in this code?

  2. Thread.sleep() throws an interruptedexception in order to catch that we have the thread.sleep(500) inside the try block.

    You can mark the 'test' method as throws exception also .

    For exceptions thrown by various method look into the javadocs.

  3. Because Selemium is executing even before page is loaded "NoSuchElementException" is usually occurred unless you sleep the thread, and give some time to load the page..
    so before every page loading event use
    Thread.sleep(6000)
    if the internet connection is slow use more than 6000 milliseconds

Leave a Reply

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