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:
- Access the DOM of webtable using .object notation.
- 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”.

- Find the count of total number of tags and loop it find out the font-color.
The corresponding VB script would be:

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!
I'm sure you have already subscribed to our feed but just in case not. I recommend you to subscribe via RSS feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.
Related posts:



19 comments ↓
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
good.
nice work ankur
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
Good to know about DOM use in QTP.
The concepts explained for DOM are quite interesting.
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
@Ajay: For questions not related to post, please use QTP forums.
Super sir,keep going
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 !!!
‘ 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
How to get font type weather it is bold or italic etc?
Thank you so much..
DOM concept is quite interesting
Ankur,
Thanks for sharing this info abt DOM. I was doing something wrong with src. It is clear now.
Thanks,
~P
How would you use the same approach if you need to find all images with a particular name? img_expand_node.gif for example?
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
please explain in detail
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
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?
Leave a Comment