Micro Focus QTP (UFT) Forums
I want to count total no of links in a page and click one by one - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: I want to count total no of links in a page and click one by one (/Thread-I-want-to-count-total-no-of-links-in-a-page-and-click-one-by-one)



I want to count total no of links in a page and click one by one - excellentpawan - 07-22-2013

Big GrinI want to count total no of links in a page and click one by one so i wrote code but this is giving gerenal error
Code:
Set objlink = description.Create()
objlink("micclass").value ="Link"
set links = browser("title:=.*").Page("title:=.*").ChildObjects(objlink)
totalnooflinks = links.count

For i =0 to links.count -1
    print links(i).click
    browser("title:=.*").Back

Next

Thanks & Regards
Pawan


RE: I want to count total no of links in a page - ravi.gajul - 07-22-2013

This is expected because when we create a reference to the object and use it for the first time, QTP cashes the object and maps it to the run time object.It does this cashing to improve performance on the same object when used later.But when the application state has changed(page refreshed or changed) these object references are no longer valid.So I would suggest you to get the reference again before you click.
This is how you would do it.
Code:
Set objlink = description.Create()
objlink("micclass").value ="Link"
set links = browser("title:=.*").Page("title:=.*").ChildObjects(objlink)
totalnooflinks = links.count
For i =0 to links.count -1
    set links = browser("title:=.*").Page("title:=.*").ChildObjects(objlink)
    links(i).click
    browser("title:=.*").Back
Next



RE: I want to count total no of links in a page and click one by one - basanth27 - 07-22-2013

I think the issue is print links(i).click

You cannot print a method action.

BTW, The cache issue does not arise as it is in runtime and still resides in the memory until and unless explictly set to a nothing.


RE: I want to count total no of links in a page and click one by one - ravi.gajul - 07-22-2013

That's correct we cannot print a method action.The code returns general error though print is excluded in the second iteration as the reference is lost due to Browser().back.
Here is a small demo to justify this statement.
Code:
Set oPage=Browser("name:=Google").Page("title:=Google")
Set oSearch=oPage.WebEdit("name:=q")
Set oSearchButton=oPage.WebButton("name:=Search")
oSearch.Set "Learn QTP"  'runs fine without any error
Browser("name:=Google").Refresh 'or back..
oSearch.Set "Learn QTP" 'throws error
Each QTP object has a hidden method "Init" to reinitialize the object cashe. Using Init on the object makes sure that it is re-identified.With QTP 11, new method "RefreshObject" has been introduced.
Code:
Set oPage=Browser("name:=Google").Page("title:=Google")
Set oSearch=oPage.WebEdit("name:=q")
Set oSearchButton=oPage.WebButton("name:=Search")
oSearch.Set "Learn QTP"  'runs fine without any error
Browser("name:=Google").Refresh
oSearch.RefreshObject 'reinitializing the object 'oSearch'
oSearch.Set "Learn QTP New" 'runs fine without any error



RE: I want to count total no of links in a page and click one by one - basanth27 - 07-22-2013

In that case it should say, "Object could not be found" and not a General Run error. You may check the object's properties during runtime using the watch statement.

Anyways, I would wait for excellent pawan to revert back as to what solved the problem.


RE: I want to count total no of links in a page and click one by one - excellentpawan - 07-22-2013

(07-22-2013, 09:28 AM)ravi.gajul Wrote: This is expected because when we create a reference to the object and use it for the first time, QTP cashes the object and maps it to the run time object.It does this cashing to improve performance on the same object when used later.But when the application state has changed(page refreshed or changed) these object references are no longer valid.So I would suggest you to get the reference again before you click.
This is how you would do it.
Code:
Set objlink = description.Create()
objlink("micclass").value ="Link"
set links = browser("title:=.*").Page("title:=.*").ChildObjects(objlink)
totalnooflinks = links.count
For i =0 to links.count -1
    set links = browser("title:=.*").Page("title:=.*").ChildObjects(objlink)
    links(i).click
    browser("title:=.*").Back
Next



RE: I want to count total no of links in a page and click one by one - ravi.gajul - 07-22-2013

Looks like the page is still not navigated to the next one and we are doing browser().back.Could you please add some more wait time ,more than 60 seconds and try again?This should work fine.Looks like we have a synchronization issue.To isolate between synchronization and code, could you run it in debug mode and check for 3- 5 links allowing sufficient time for the page navigation.Let me know your findings.


RE: I want to count total no of links in a page and click one by one - excellentpawan - 07-23-2013

using wait property it works now plz help me if i want to click on perticular link one by one with same name then how to do I am attaching a picture in which links is present


RE: I want to count total no of links in a page and click one by one - excellentpawan - 07-23-2013

i have done it


RE: I want to count total no of links in a page and click one by one - Ankur - 07-23-2013

@excellentpawan: While attaching the screenshots, please ensure to re-size them and attach the part which is necessary. Reffering to