QTP: Setting WebEdits within a WebTable using ChildItem method is not working for rows other than first row

qtp

I am using Webtable ChildItem method in QTP to set the WebEdit and WebCheckbox and has following issues

  1. In First row it sets the value for 2nd Column (WebEdit) and 4th column (WebCheckbox) properly but for 3rd Column (WebEdit) it sets the value and as soon as it moves to fourth column resets it to old value.

  2. Also for second row onwards it gives error as

    Object required: 'WebTable(…).ChildItem(…)'

Please guide me in this matter.
Here is the code I am using:

Set objFrame = Browser("Browser").Page("Page").Frame("Frame")
If objFrame.WebTable("WebTable").Exist(0) Then
    rowct = objFrame.WebTable("WebTable").RowCount
isFound = 0

 For i= 2 To rowct
    strText = objFrame.WebTable("WebTable").GetCellData(i,1)
    index = i-2
    If Instr(strText,strType) > 0 Then
        objFrame.WebTable("WebTable").ChildItem(i,2,"WebEdit",index).Set strNumber
    objFrame.WebTable("WebTable").ChildItem(i,3,"WebEdit",index).Set strNumber2
    objFrame.WebTable("WebTable").ChildItem(i,4,"WebCheckBox",index).Set strPreferred

       isFound = 1
   Exit For
 End If
   Next

   If isFound = 0 Then
       rowct = objFrame.WebTable("WebTable").RowCount
   row= rowct + 1
   index = row - 2
   objFrame.WebButton("Button").Click

       objFrame.WebTable("WebTable").ChildItem(row,1,"WebList",index).Select strType
       objFrame.WebTable("WebTable").ChildItem(row,2,"WebEdit",index).Set strNumber
       objFrame.WebTable("WebTable").ChildItem(row,3,"WebEdit",index).Set strNumber2
       objFrame.WebTable("WebTable").ChildItem(row,4,"WebCheckBox",index).Set strPreferred

End If
End If

Best Answer

This is the cause for the error you get:

For i= 2 To rowct
    strText = objFrame.WebTable("WebTable").GetCellData(i,1)
    index = i-2
    If Instr(strText,strType) > 0 Then
        objFrame.WebTable("WebTable").ChildItem(i,2,"WebEdit",index).Set strNumber
    ...

The fourth argument of the ChildItem method on a WebTable is the index of the returned childitem (WebEdit in this case) on that row / column combination.
So, what is happening: On the first found row with content (nr. 2), the index becomes 0, but on the second iteration of the for loop, the index becomes 1. QTP cannot find a second WebEdit object on that row / column and will return an error.

So, use index = 0 to solve that one.

For your first question, what happens if you change the order: Set the WebCheckBox first and then the second WebEdit:

objFrame.WebTable("WebTable").ChildItem(i,2,"WebEdit",index).Set strNumber
objFrame.WebTable("WebTable").ChildItem(i,4,"WebCheckBox",index).Set strPreferred
objFrame.WebTable("WebTable").ChildItem(i,3,"WebEdit",index).Set strNumber2

Can you tell what is happening now?

Related Topic