Thursday, March 24, 2011

How to retrieve all the link names and URLs using QTP?

We need to use the ChildObjects method to get the collection of links. Using the collection object we can then retrieve the name and URL properties of every link.

Step by Step illustration:

1) Using the ChildObjects method, get all the links on the webpage.
2) We now have the collection object ready.
3) Use the GetROProperty method to retrieve the required property values. (In our case Innertext & href)

Example :
' This can be used on any page
Set oLinkDesc = Description.Create()

' Retrieve HTML tag "A"
oLinkDesc("html tag").Value = "A"

' Create the collection object
Set nLinksCollection = Browser("title:=.*"”).Page("title:=.*").ChildObjects(oLinkDesc)
nLinksCount = nLinksCollection.Count()

' Retrieve all the requied properties
For i=0 to nLinksCount-1

   sName = nLinksCollection(i).GetROProperty("innertext")
   sHref = nLinksCollection(i).GetROProperty("href"”)

   ' Saving the results in the reporter
   Reporter.ReportEvent 0, "Links name: " & sName & "; URL: " & sHref


Click here for more QTP code snippets.


  1. Above code will not work if you open multiple browsers. Check it if you want.


  2. Yes, it may fail. But, as a best practice, we always have only one browser instance open. We should be aware of the fact that QTP gets confused when there are multiple instances of the application open (on multiple browsers). However, there are ways of handling it, but not advisable.

  3. Hi

    On running the above script, I am receiving a Syntax error for the below line:

    Set nLinksCollection = Browser("title:=.*"”).Page("title:=.*").ChildObjects(oLinkDesc)

    Could someone please take a look? I have a single browser open ( that is Firefox and all scripts run on IE.)

    1. Hi there, it is not a good idea to try in FF. IE is the best supported browser by QTP.

    2. u have to give the Exact browser class name and the page name , after that it wont gives the syntax error

  4. How to export link name and urls to an excel sheet rather than using Reporter.reportevent?


Please leave your comment here...