Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I want to count total no of links in a page and click one by one
#1
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
Reply
#2
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
Reply
#3
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.
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#4
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
Reply
#5
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.
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#6
(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


Attached Files Image(s)
   
Reply
#7
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.
Reply
#8
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


Attached Files Image(s)
   
Reply
#9
i have done it
Reply
#10
@excellentpawan: While attaching the screenshots, please ensure to re-size them and attach the part which is necessary. Reffering to
Want to fast track your QTP/UFT Learning? Join our UFT Training Course
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  click each web link in a page using Index or other method in QTP smeijer 13 26,698 04-29-2017, 04:53 PM
Last Post: kowshik
Question Webtable contains cell contains two links, how to click on link srinivassure 9 55,451 07-30-2016, 03:41 PM
Last Post: Akshay
  VB Script to count number of links in a web page. Suma Parimal 4 26,380 07-01-2015, 12:59 PM
Last Post: govind
  This code does not give total no of links present on page plz help it returns 0 excellentpawan 8 6,992 01-17-2015, 07:49 PM
Last Post: yogi4tech
  Right Click On a Image and Click on a Link in WebElement RameshKrishnan 4 6,224 07-10-2013, 04:13 PM
Last Post: RameshKrishnan

Forum Jump:


Users browsing this thread: 1 Guest(s)