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.

«October»
SunMonTueWedThuFriSat
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234