Hi pawan, even though your post is old from 2013 i just joined and wanted to give you a solution to your problem. Or people like you to troubleshoot if your code returns 0 as a value of link present on a web page
You could do it few different ways the simplest way to do this is, to use Regular Expression which can count link present on any browser which is open and count the links.
Please note that you may have multiple browsers running on your computer or local machine such as chrome, firefox and IE at the same time. Therefore if you want to count links present on google.com which is opened on internet explorer. Do these steps.
1. go to the task bar and right click on internet explorer
2.again select the internet explorer highlighted icon and right click .
3. you will see a dialogue box open and you will see an option "RUN AS AN ADMINISTRATOR"
4. Click on run as an administrator.
if you don't run the browser as an administrator the QTP doesn't invoke the browser and the webpage available within that browser, so even though the syntax of the script is ok it returns the value as "0"
here I am giving you the script which not only will return the number of links present but also it will print on datasheet and it will export the datasheet to an excel file in your C drive. try this and hope this Helps.
And don't listen to people like ANkur who they put newbies down by insulting them. They actually forget that they were new to this field sometimes back.
Peace and here is the code.
Code:
Set oDesc = Description.Create()
oDesc("micclass").Value = "Link"
'Here is the regex for any webpage which is open on a browser
Set Links = browser("creationtime:=0").page("title:=.*").ChildObjects(oDesc)
print "Total links present: " & Links.Count
' here is to print the link names to datasheet in qtp
datatable.GlobalSheet.AddParameter"linkNames",""
'obj.Count value has the number of links in the page
For i = 0 to Links.Count - 1
datatable.SetCurrentRow(i+1)
' get the name of all the links in the page
datatable("linkNames") = Links(i).GetROProperty("name")
datatable.Export"c:\Nanu1_Links.xls"
Next