Document Object Model(DOM) and QTP – A complete guide.

What is Document object Model?

Wikipedia defines Document Object Model(DOM) as:

A platform- and language-independent standard object model for representing HTML or XML and related formats.

For QTP’s sake, I would redefine it to make it simpler. DOM is a method for QTP engineers to access the source (IE –> View –> Source) of any webpage direct through VB Scripting.

When can we use DOM?

One of the very important places you can use it is when a QTP web table checkpoint doesn’t show you the desired content in a cell. I mean the content in the cell is in a format that is not supported by the web table checkpoint. Another use can be when you want to access all the HTML tags used in a webpage. You can get the names, the innertext, innerHTML property of all these tags. The possibilities are endless.

How can we use DOM to access the source page?

We can access the source page of any webpage using .object notation.

Any practical example of using DOM?

I have created a demo web page to show you document object model in action. Say you want to find the font color used for writing Happy Holidays Everyone in cells of the webtable given on this page. The algorithm would be:

  1. Access the DOM of webtable using .object notation.
  2. Access the tagname corresponding to the property you wish to find out. In our case tagname is “font” and property to be explored is “color”. Document Object Model QTP
  3. Find the count of total number of tags and loop it find out the font-color.

The corresponding VB script would be:

Using DOM to find the font color

Now it’s quiz time. On the same page find out the src of all the images used in the table. Let me know your script through the comments below.

[Also note that src won’t show up in the webtable checkpoint (for obvious reasons!)]

So, what does src say to you?

I wish you Happy Holidays 2009! :)

Ankur Jain

Founder at LearnQTP
Loves blogging and automation.

49 comments ↓

#1 sameer on 12.26.08 at 23:06

Hey it was very simple…. But thanks for your inputs on the DOM since we use the webtables to a very great extend here also with the lot of combination. thoguh I have not tried implementing it on my app I am sure this one will make our life easy.

Set Font = Browser(“Browser”).Page(“Page”).WebTable(“Happy”).Object.all.tags(“img”)
msgbox Font.length
For i =0 to Font.length -1
msgbox Font(i).src
Next

#2 Anilkumar on 12.27.08 at 05:55

good.

#3 maddy on 12.27.08 at 08:32

nice work ankur

#4 Sankar on 12.27.08 at 10:08

here is the script to find the src of the images in the given web table “Happy Holidays Everyone!”

Set img = Browser(“Browser”).Page(“Page”).WebTable(“Happy”).Object.all.tags(“img”)

msgbox img.length

For i=0 to img.length-1

msgbox img(i).src

Next

#5 kiran on 12.29.08 at 01:37

Good to know about DOM use in QTP.

#6 Yogindernath on 01.09.09 at 02:46

The concepts explained for DOM are quite interesting.

#7 Ajay on 01.13.09 at 02:25

Hi Ankur,
Your articles are so helpful.
Could you explain about What DOM is all about. And also the code that u gave for comparison of 2 Excel sheets cell by cell, in that u used code like ” For each cell in WorkbookSheet1.usedrange ”
How to use this code and same sort found in finding the names of subfolder. Could explain about this plzz…

Thanks in Advance.
Regards,
AjayKumar

#8 Ankur on 01.13.09 at 02:49

@Ajay: For questions not related to post, please use QTP forums.

#9 Ravee on 02.03.09 at 03:21

Super sir,keep going :-)

#10 Rohit on 03.09.09 at 01:30

Set obj_link = Browser(“ClientLink Cash Entry:”).Page(“ClientLink Cash Entry:”).WebTable(“State”).Object.cols
msgbox obj_link.count

help me this code is not working !!!

#11 Baba on 03.12.09 at 22:46

‘ To get the Source of images for this webpage “http://toankurjain.googlepages.com/DOM.html”

‘Regular method

Dim img

Set img=description.Create
img(“micclass”).value=”Image”

Set brimg=browser(“Browser”).Page(“Page”).ChildObjects(img)
msgbox brimg.count

For i=0 to brimg.count-1

print brimg(i).getroproperty(“src”)

Next

‘ DOM Method

Dim obj

Set obj=browser(“Browser”).Page(“Page”).WebTable(“Happy”).Object.all.tags(“img”)

msgbox obj.count

For i=0 to obj.count-1

sourc=obj(i).src
print sourc

Next

#12 TRMR on 03.16.09 at 20:53

How to get font type weather it is bold or italic etc?

#13 nandu on 04.02.09 at 16:04

Thank you so much..
DOM concept is quite interesting

#14 Pagkis on 04.18.09 at 23:20

Ankur,

Thanks for sharing this info abt DOM. I was doing something wrong with src. It is clear now.

Thanks,
~P

#15 Tatyna on 06.04.09 at 09:22

How would you use the same approach if you need to find all images with a particular name? img_expand_node.gif for example?

#16 Mahalakshmi on 08.11.09 at 04:59

set img_no=Browser(“Browser”).Page(“Page”).WebTable(“Happy”).object.all.tags(“img”)
for i=0 to img_no-1
print img_no(i).src
next i

#17 puli jay on 08.11.09 at 09:18

please explain in detail

#18 Kay on 09.27.09 at 08:49

Hi,

Is it possible to use DOM to get any string within the view source of a web page? I’m trying to target just specific strings and put them into an excel sheet.

Any help appreciated,
K

#19 Franklyn on 10.13.09 at 19:37

Hi

i have one query ,

in my assignment i have the following code

here the tag name is ‘INPUT’

i want to retrieve the attribute ‘checked type’ s value ..
in qtp if place the statement msgbox font(i).checked type

QTP throws syntax error, since ‘checked type’ has a space between ‘checked’ & ‘type’ .

Can any one help me how to retrieve the ‘checked type’ value?

#20 Himanshu on 04.23.10 at 10:57

Can neone helpme out by telling the link of the website containing all the methode nd properties that can be used / applied in the DOM method.

#21 Neelakantan on 04.23.10 at 16:47

Perfect!. Very simple but more effective

#22 Kenneth on 05.31.10 at 17:04

Hi when i use the below code it crashes QTP…can you tell me why?

The goal of this code is to capture the name of all the links on the page…

Set a = Browser(“title:=.*”).Page(“title:=.*”).Object.Links

For i = 0 to a.Length -1
print a(i).name
Next

#23 NatTurn3r on 06.09.10 at 04:17

@Kenneth

Try this instead…

For i = 0 to a.Length -1
myString = a(i).name
print myString
Next

#24 KD on 06.10.10 at 23:29

Hi Genius minds,
I have a small question. How I can check format of specific cell in webtable.

For Example, I have a table with 3 row and 3 column.
I want to check what is the cell (2,3) format for 2nd row and 3rd column. I want to verify if its plain text or link.

I hope I will get the answer. :)

Thanks,
KD

#25 Kenneth on 06.16.10 at 18:29

Thanks that worked!! :)

one more thing…

Browser(“hwnd:=” & hwnd).object.document.getElementsByTagName(“meta”).length

Why do i get General run error for this? and how do i fix it.

#26 Kenneth on 06.16.10 at 18:41

The actual other code is..like so…
Browser(“hwnd:=” & hwnd).object.document.getElementsByTagName(“meta”)(i).content

i am trying to capture the contents in in the tag…and not its properties…

was trying another method using getElementsByTagName…

Please adivce…

#27 SanMan on 07.29.10 at 17:11

If i have a tag like tagval how could I retrieve the “tagval” ??

Set objname = Browser(“..”).Page(“…”).Frame(“…”).Object.all.tags(“sometag”)

For i =0 to objname.Length -1
MsgBox objName(i).
Next

Thanks in advance…

#28 SanMan on 07.29.10 at 17:41

@SanMan
Found it…”innertext” does the trick

MsgBox objName(i).innertext

#29 Litan on 08.01.10 at 13:46

Ankur,

Continue your nice work.

#30 venu on 08.02.10 at 17:00

Hi Ankur,
your efferts are fabulous.

could you please suggest me how to handle this,
i have a webtable,the cells in which some times identifying as webelement and some times as webedit , so i am not able to handle the situation, could you please send me any suggestions that helps me.

#31 Vaughn Fuller on 08.27.10 at 03:30

I am relatively new to QTP and I am having issues identifying objects on a specific webpage. The object spy displays editable fields in the page as WebEdit fields and I am able to Set the values in these fields when I have the object in the repository and in my script set to the proper heirarchy values. When a given field is entered, the screen dynamically adds fields and these new fields are identified as WebElements. If I set up my code and the object repository to recognize these fields as WebElements, then I can not use the .Set method, becaus this method is not supported for WebElements. Is there a general way of identifying the heirarchy of elements in a web page that actually works and provides the user with a recognizeable object that can be set to a specified value?

#32 ana on 10.29.10 at 14:36

Hi, when im run the below query, Im getting the output as “[object]“, Pls help me how to get the ‘style’ value in “img” tag in html.
set font = Browser(“Browser”).Page(“Page”).WebTable(“Happy”).Object.all.tags(“img”)
For i=0 to font.length-1
set a = font(i).style
msgbox a
Next

#33 guru on 01.19.11 at 11:51

its working fine

#34 Ritu on 02.02.11 at 16:46

Hi….can somebody help in GIS application automation using QTP.This is a web application and uses Google map. QTP is not identifying any objects in on the MAP and we have to details with various map events like Zoom in, Zoom out, selecting an area on the map etc like we do in Google map.

#35 sujatha on 03.15.11 at 02:18

How to verify whether a webelement is enabled or not using view source?

#36 Pranitha on 08.24.11 at 15:18

Hi.. Can any one tell me how to test the Maps (ex:google earth)

#37 Fahim Aslam on 09.23.11 at 16:09

how to get the innerText of a div(html tag) using VBScript?

#38 Fahim Aslam on 09.23.11 at 16:44

can we add objects to object repository at run time in qtp?

thnx in Advance

#39 prajesh on 12.05.11 at 19:46

how to use DOM in a windows based applications(like Delphi, SAP, etc) or we cant use DOM in Windows ased applications

#40 Ramya on 12.19.11 at 16:53

I want to check a text in source code of a web page.How to do that?

#41 Mahesh NP on 01.05.12 at 12:08

Hi

I have one query .

How to find the no of links in Webpage.

There is two approach for this
1. browser(“Google”).Page(“Google”).Object.links.length

2. To Create the DOM object

Set oDesc = Description.Create()
oDesc(“micclass”).Value = “Link”
Set Links = Browser(“Browser”).Page(“Google”).ChildObjects(oDesc)
NumberOfLinks = Links.Count()
Msgbox NumberOfLinks

From the above 2 code .Im getting different answer after execution.Please let me know what is difference between link.lenght and link.count.

Thanks in advances

#42 sk on 02.17.12 at 08:15

Mahesh,

try this…

Set Links = Browser(“Browser”).Page(“Google”).Object.all.tags(“a”)
linksCount = Links.length
‘print number of links in the page
msgbox linksCount

#43 Bandi sekhara reddy on 03.16.12 at 13:22

HI ,
When are the situation go for Dom Object model apart from the childobjects normal model….?

And also please provide the any methods are there to identify the Back Ground colour and Highlighted with the Different colrs of perticular elements

Thanks

#44 Kiran on 03.19.12 at 17:50

I want to know the color of error message font color

#45 Rajneesh on 07.22.12 at 11:46

How can we change front color of a text at run time?

#46 Gajendra on 07.31.12 at 15:40

Hi,

I want to read the value from the application by passing the Label .
Say suppose i create an account by entering all the data and click on create, It navigates to View account page. The data is displayed as follow

Label Value
Account Name actName1234
System Account ID 7124712

The Html look like this

Account ID

INTRAWARE

System Account ID

7124712

Account Name

Intraware, Inc

Account Status

ACTIVE

Account Note

Content

Number of Active Members

79

I want some generic function where i would pass “Account Name”, It should return me “7124712″

Regards,
Gajendra

#47 how to see the htmlcode of theweb page on 11.04.12 at 09:12

to use the tag to identify the object you must know what hml tags are presen in a web page.plz any one can say how to see html code so that it can be used in witting the script .
thanks in advance
Roli

#48 Karthik on 11.26.12 at 09:49

Hi Ankur,

I have a samll query, Is that possible to get all the web elements(textbox, button) present inside a particular DIV?. If so, Can you please tell me how to do that?

Thanks in Advance

#49 Naresh on 03.27.13 at 08:53

Hi Ankur,

i am not able to find the index of the webtable which is changing the index dynamically .

Leave a Comment