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 comments:

  1. I tried the above code but it is not working for me

    ReplyDelete
  2. I am getting NoSuchElementException.. Plz help me on This..

    ReplyDelete
    Replies
    1. 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

      Delete
  3. While locating element , I am getting NoSuchElement exception... what to do for this?

    ReplyDelete
  4. While locating element i am getting NoSuchElement exception.. how to overcome this..?

    ReplyDelete
    Replies
    1. Let me know your email ID so that I your solve this Issue online

      Delete
    2. Even I am also getting this error.PLEASE HELP....

      Delete
    3. Increase Implicit wait to 20 seconds as bellow.
      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

      Delete
    4. Many Thanks!!!Worked for me....

      Delete
  5. 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?

    ReplyDelete
    Replies
    1. for loop Is placed to check the status of button's attribute after every half minutes

      Delete
  6. Hi, As beginner in selenium i want to ask why "Thread.sleep(500)" is putted inside Try Block.

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

    ReplyDelete
  8. Thanks for your tutorials

    ReplyDelete
  9. does this code needs any imports? It just doesn't run for me..I'm wondering if it needs any API imports?

    ReplyDelete