Delivered by FeedBurner

Locating Web Element By ClassName In Selenium WebDriver with example

There are many different ways of locating elements in webdriver for your software web application page and I have described one of them in my PREVIOUS POST with example. In webdriver, we can't do any thing on web page of software web application if you don't know how to locate an element. As you know, we can use element's ID to locate that specific element but suppose if your element do not have any ID then how will you locate that element on page of software application? Locating web element by className is good alternative if your element contains class name. We can locate element By Tag Name, By Name, By Link Text, By Partial Link Text, By CSS and By XPATH too but will look about them in my next posts.

Create selenium webdriver data driven framework from scratch @This Page.

How to get the class name of element
You can get the class name of element using firebug add-on software as shown in bellow given image.

Look in to above image. Post date content has a class and we can use that class name to store that blog post date string. Notice one more thing in above image, blog post date string has not any ID so we can not locate it by ID.

(Note : You can view more webdriver tutorials on THIS LINK.)

Example Of Locating Web Element By ClassName
We can use bellow given syntax to locate that element of software web application.


Practical example of locating web element by className is as bellow. 
package junitreportpackage;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Mytest1 { 
 //To open Firefox browser
 WebDriver driver = new FirefoxDriver();
 public void beforetest() {

 //To Maximize Browser Window

 //To Open URL In browser
 public void aftertest() {
 public void test() 
   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
   String  datentime = driver.findElement(By.className("date-header")).getText();//Locating element by className and store its text to variable datentime.

In above example,

Open Browser
WebDriver driver = new FirefoxDriver() will open webdriver's Firefox browser instance.

Open URL
driver.get(); will open targeted URL in browser.

Maximize Browser Window
driver.manage().window().maximize(); will maximize browser window.

View THIS POST to know how to run webdriver test in google chrome and View THIS POST to know how to run webdriver test in IE browser..


  1. if there is a link on the page and we want verify the text of that link is it necessary to use link locator

  2. May I ask what application do you use to view the object and the UI instantly at the same time?
    Or do you know something one which is open source?
    I think this would be a big help to those who use selenium web driver.
    Thanks a lot for your tutorials...

  3. What application do you use to view the source code and UI instantly at the same time?
    Or do you know an open source application to do this?
    I think this will be a great help to locate any related IDs, Name etc in a particular object.
    Thanks a lot for the tutorial.

  4. Never mind, I didn't notice the word FIREBUG...
    Thanks anyway...

    1. These posts will help you

  5. Hi,
    I got proper knowledge on selenium Web Driver by your tutorials.
    Thank a lot