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

Updated On Aug 8, 2014 by

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 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! :)

Welcome new readers: Download the Free eBook!

Ankur JainI am Ankur Jain, the owner of LearnQTP and founder of a software organization. This site is run, maintained and updated by me and my team. Feel free to take a look around, you may want to start with What is QTP? 

You can connect with me on my Google+ page. 

Please enter your first name and email address below to instantly download the eBook on Optimizing QTP Scripts. In future, we will make sure you get new tips & tricks on QTP delivered direct to your email box.



Share This:

51 Comments

  1. aswin

    u can try the below given code…

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

  2. Naresh

    Hi Ankur,

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

  3. Karthik

    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

  4. how to see the htmlcode of theweb page

    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

  5. Gajendra

    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

  6. Rajneesh

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

  7. Kiran

    I want to know the color of error message font color

  8. Bandi sekhara reddy

    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

  9. sk

    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

  10. Mahesh NP

    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

  11. Ramya

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

  12. prajesh

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

  13. Fahim Aslam

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

    thnx in Advance

  14. Fahim Aslam

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

  15. Pranitha

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

  16. sujatha

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

  17. Ritu

    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.

  18. guru

    its working fine

  19. ana

    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

  20. Vaughn Fuller

    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?

  21. venu

    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.

  22. Litan

    Ankur,

    Continue your nice work.

  23. SanMan

    @SanMan
    Found it…”innertext” does the trick

    MsgBox objName(i).innertext

  24. SanMan

    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…

  25. Kenneth

    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…

  26. Kenneth

    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.

  27. KD

    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

  28. NatTurn3r

    @Kenneth

    Try this instead…

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

  29. Kenneth

    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

    • Sreedhar

      If you want to get name of the object then use GetRoProperty(“name”) then you will get the name of the object.

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

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

  30. Neelakantan

    Perfect!. Very simple but more effective

  31. Himanshu

    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.

  32. Franklyn

    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?

  33. Kay

    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

  34. puli jay

    please explain in detail

  35. Mahalakshmi

    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

  36. Tatyna

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

  37. Ankur,

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

    Thanks,
    ~P

  38. nandu

    Thank you so much..
    DOM concept is quite interesting

  39. TRMR

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

  40. ‘ 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

  41. Rohit

    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 !!!

  42. Ravee

    Super sir,keep going :-)

  43. Ankur

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

  44. Ajay

    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

  45. Yogindernath

    The concepts explained for DOM are quite interesting.

  46. kiran

    Good to know about DOM use in QTP.

  47. Sankar

    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

  48. maddy

    nice work ankur

  49. Anilkumar

    good.

  50. sameer

    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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>