QTP: Dynamic Links

A user on the QTP forum posted this question. I tried to provide an example on the forums, but alas, too many characters. So, thought I'd post it here, but off the main feed.

Dynamic Link problems

I'm having problems clicking a dynmaic link while playing back a QTP 9.0 script. The location of the link I'm clicking on can vary each time the script is ran. Also, there may be many links on the page with the same name. The only unique attribute changes each time the dynamic link is created. Help!!

Dynamic links are troublesome, to be sure. Your case where the link to the left (or right) identifies the correct link to click makes it particularly tough. When I've run into that, I've used the DOM to great success. Here's what I might try (and have used) before in dynamically generated webtables (like a query result). Naturally, this is a LOT of code, but sometimes, doing it the hard way gives you a tremendous amount of flexibility and enables you to compensate for dynamic pages. Bear in mind also that if the anchors have id properties (too much to hope for most of the time), you can also grab the Anchor by GetElementById method.

First, add the table to your OR. You don't have to do so, techincally, but it saves you a ton of code.

From there, you can use something like:

Dim oAnchors
Dim oAnchor
Dim blnFound

Set oAnchors = Browser(X).Page(X).WebTable(X).Object.GetElementsByTagName("A") 'This will grab all of the links in the page.

blnFound = False

For Each oAnchor in oAnchors
  'Use some property to identify which link you are on
  'And if it's the right one....
  If oAnchor.innertext = "Whatever" Then
     oAnchor.Click()
     blnFound = True
  End If
  Set oAnchor = Nothing
  If blnFound = True Then Exit For
Next 'oAnchor

Set oAnchors = Nothing

If you need to check cells located in the same (or different) row, then you can grab the rows and cells in a similar fashion:

This example assumes we are checking the first cell of a row, and then interacting with the third cell, where we expect the links

Dim objRows
Dim objRow
Dim objCells
Dim objCell
Dim blnFound
Dim lngCellCount

Set objRows = Browser(X).Page(X).WebTable(X).Object.GetElementsByTagName("TR")

blnFound = False
lngCellCount = 0

For Each objRow in objRows
   Set objCells = objRow.GetElementsByTagName("TD")
   For Each objCell in objCells
      'Use some property to identify the correct row/cell
      If objCell.InnerText = "ThisOne!" Then
         blnFound = True 'We're on the correct row
      End If
      'If it's found we want to start counting rows
      If blnFound = True then lngCellCount = lngCellCount + 1
      If lngCellCount = 3 Then 'The third cell
         'Use the first example to get all of the links in the table cell, and to click on the one you want
      End If
      Set objCell = Nothing
      If blnFound = True Then Exit For
   Next 'objCell
   Set objCells = Nothing
   Set objRow = Nothing
   If blnFound Then Exit For
Next 'objRow

Set objRows = Nothing

This might (and probably does) need some tweaking as I wrote it off the top of my head (such as it is). In any event, perhaps you'll find it useful. Hopefully someone can think of an easier way (I sure hope) and can share it.

Oh, and check http://www.w3schools.com/htmldom/dom_reference.asp for more information on the DOM and how to use it.

Theo

 

EDIT (02/12/2007): The examples needed a slight bit of tweaking. Per my disclaimer ("This might (and probably does) need some tweaking"), an adjustment was required. The statements above referring to using the "GetElementsByTagName" method have been modified to include the "Object" modifier, which allows us to refer to the XMLDOM element underneath. Adding that will make this run fine. Thanks to all you pointed out that it wasn't working.

posted @ Wednesday, October 11, 2006 12:13 PM

Print

Comments on this entry:

# re: QTP: Dynamic Links

Left by Tester at 2/9/2007 6:44 PM
Gravatar
Hi,

When I try to utilize GetElementById method mentioned in the first example I get following error:

Object doesn't support property or method:'GEtElementsByTagName'

Any ideas why I'm getting it?

Thank you.

# re: QTP: Dynamic Links

Left by Theo Moore at 2/9/2007 9:08 PM
Gravatar
Not off the top of my head. Did you add the Webtable to the object repository? Unfortunately, I am not at my work machine this weekend to take a quick peek at it.

# re: QTP: Dynamic Links

Left by Nageswara Rao at 2/12/2007 5:47 AM
Gravatar
hi moore,

even i am also getting the same error when i use getelementsbytagname method. please give a reply

thank you

# re: QTP: Dynamic Links

Left by Theo Moore at 2/12/2007 6:20 AM
Gravatar
I've updated the examples in this post to reflect the correct way to refer to the GetElementsByTagName method. As stated, what I wrote was off the top of my head and required some adjustment. :-) Thanks for pointing out the issue.

# QTP: Dynamic Links

Left by hak at 12/7/2007 5:01 AM
Gravatar
Hi,
I need urgent solution for this problem. I've to click the fisrt link of yahoo inbox.Subject of the mail is always changing. How to write descriptive program for this problem?anyone can help?

# QTP: Dynamic Links

Left by hak at 12/7/2007 5:01 AM
Gravatar
Hi,
I need urgent solution for this problem. I've to click the fisrt link of yahoo inbox.Subject of the mail is always changing. How to write descriptive program for this problem?anyone can help?

# re: QTP: Dynamic Links

Left by Basavaraju at 5/2/2008 5:47 PM
Gravatar
It helped
Comments have been closed on this topic.
«December»
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910