Selenium – Understanding Object Identification

by seetaram on February 23, 2011

In previous two posts I have explained how to setup a sample web application, recording and playing back tests with Selenium IDE.  Now, we will understand how Selenium identifies the objects on the Application Under Test.

To identify the objects such as Links, Buttons, Edit boxes, Drop downs, etc on the application Selenium uses a concept called “Locators”.  There are around 8 different types of locators.  Out of them, I will be explaining only four as they are widely used while automating the test cases using Selenium.

  1. id = id – By default Selenium IDE takes “id” as Locator

Every Web Page will be having some HTML Source code.  This can be viewed using “View –> Page Source / View source” on the browser. In the following picture we can see “id” attribute of a HTML tag is used as a locator to identify an object.

Locator-id

2.  name = name (Second locator)

We will do the following steps to understand how exactly Selenium identifies the objects:

  1. Start Selenium IDE from Tools menu of the browser
  2. Login to sample application – Sugar CRM
  3. Click on Accounts tab
  4. Click on Create Account link
  5. Now Create Account Page gets displayed

Selenium IDE has recorded all these steps.  In the following pictures all the steps above can be seen.

viewsource-1

viewsource-3

In the following picture we can see the “name” property of the object is used as Locator to identify the object.

viewsource-2

3. xpath = xpathExpression.  Xpath is used as a Locator to identify an object.  This is an expression which is formed by combining various HTML tags.  For example: //a[contains(text(),’VRL travels’)], //div[@id=’shortcuts’]/span/span[2]/a/span

xpath

4.  link=link text (in HTML source we can located this using “href” tag)

linkLocator

I hope you have understood how Selenium identifies objects using different Locators with all of the above screenshots & explanation.

Please provide me your valuable suggestions through comments.

{ 21 comments… read them below or add one }

vinit March 7, 2012 at 5:11 pm

Hey Sitaram,
wonderful job man… Thanks for tremendous help

I have interview tomorrow can u help me how to call scripts in selenium and
> – How to execute scripts using various browsers.
I will never forget your help.

Thanks i advance

Reply

gopalarao April 8, 2012 at 7:59 am

hi Sir i am gopi new to selenium please help me how i can learn selenium online.

Reply

seetaram April 16, 2012 at 12:25 pm

Go to Selenium – page created on this blog.. it is like index.. go through the posts one by one… and do whatever is explained.. I hope you will definitely be able to learn the selenium easily..

Reply

Murali Krishna May 18, 2012 at 9:59 am

Hay Sitaram,

You done a wonderful job and this is very helpful for beginners to understand the Selenium.
The way which you are explaining the topics are simply superb.

Could you pls add some more information into it; like coding using JUnit, TestNG, Ruby and other tools.

Thank you very much,
Murali Krishna

Reply

seetaram May 18, 2012 at 3:55 pm

Thanks a lot Murali… trying to update the blog as much as possible…

Reply

Sesha Reddy April 7, 2013 at 3:53 pm

Hello Sitaram,

Thank U Very Much for Providing this Information.This is most helpful to the selenium beginners .

Reply

raj May 25, 2012 at 2:23 pm

hi
i’m very new to selenium , so if my doubt is silly please excuse me , in the syntax for xpath what actually the span is, n what is the use of this object identification

Reply

seetaram May 25, 2012 at 3:02 pm

raj,

no worries.. let me explain you the xpath. In HTML source we can find lot of html nodes / tags. If you look at the source from top to bottom, and start combining them one after the other to reach a particular element that is xpath. Span is a HTML tag. Both Div and Span are used in html source which are primarily used to different blocks on the web page. You can read further on these tags in http://www.w3schools.com/

I hope this answers your question.

— Seetaram

Reply

vijay June 25, 2012 at 12:36 pm

HI seetharaman,

Is it possible to convert manual test cases to selenium scripts through any framework. Help me that my question is right , im going in a right way. Is it possible to convert manual test cases to selenium test cases in order to reduce human efforts.

Reply

seetaram June 29, 2012 at 4:05 pm

Answer to your question is YES!! Selenium will reduce the manual testing effort….

Reply

SRINIVAS June 27, 2012 at 11:40 am

Hi Seetharam,

Your blog is so helpful, I am able to understand very easily. Thankful to you

Regards
Srinivas

Reply

seetaram June 29, 2012 at 3:59 pm

Thanks Srinivas…

Reply

techie September 15, 2012 at 12:22 am

You find the simplest way to explain it!
Thanks a lot

Reply

Vanitha September 15, 2012 at 8:50 am

Hello Sir,

This is Vanitha. Could you pls tell me how to explain the selenium FW in interview…?

Thanking you.

Reply

Ramana October 9, 2012 at 4:25 am

Hello Sir,
This is ramana. I want to learn selenium but i am very poor in java. Is there any effect on me if i dont know that?. Is there any way to quickly learn selenium . I just saw u r website. Its good. I want this concepts little more clearly. R u providing any materials here to learn?

Reply

Surndra November 5, 2012 at 8:33 am

Hi Sitaram,

Your way of explanation and examples are very useful for everyone but i think you forgot to explain object identification using CSS. Selenium is browser independent for that reason everyone would prefer to CSS. If you explain object identification using CSS that is very useful.
And this is my suggestion only.

Thanks,
Surendra

Reply

seetaram August 22, 2013 at 6:31 am

Thanks Surendra.. I will definitely do that…

Reply

srinu reddy November 15, 2012 at 3:20 pm

Hi Ram,

this blog very helpful to me , you explanation good.

Thank you

Reply

shailaja December 14, 2012 at 9:03 am

sir,
pls explan how to config web driver in eclipse and how to execute
how to config selenium grid and how to execute
thank you.

Reply

Abhi February 12, 2013 at 11:54 am

Very helpful blog…..Great job…

Reply

anil November 28, 2014 at 10:26 am

Hi Sir
your explanation is very useful to everyone.

Reply

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email