转贴:http://geekswithblogs.net/tmoore/archive/2005/10/11/56669.aspx
So, here I am working on our VBscrīpt communication shell wrapped around QTP for testing (for all of you QTP purists who would scoff at the use of this, read some of my earlier posts about why we use this) and I realize I need a way to handle nested tables. Now, the problem really is this: I have nested tables used to format some pages. Trouble is that deep down in there, there is an image on which I need to click. So, how to get there dynamically? I need code that will search the tables and find the particular thing for which I am looking, be it a string, or an object of some sort.
Well, I have a way on which I am working that seems to be the right track, and it looks something like this:
Set ōDesc = Descrīption.Create()
oDesc("html tag").Value = "TABLE"
Set ōChild = Browser("Browser").Page("Page").ChildObjects(oDesc)
iTableCount = oChild.Count
Msgbox "We have " & iTableCount &" tables"
For lCount = 0 to iTableCount - 1 'Zero-based
For lRowCount = 1 to oChild(lCount).RowCount
'Msgbox "Table " & CStr(lCount) & " has " & oChild(lCount).RowCount & " rows and " & Cstr(oChild(lCount).ColumnCount(lRowCount + 1)) & " columns."
For lColumnCount = 1 to oChild(lCount).ColumnCount(lRowCount)
Msgbox "Current Cell: " & lRowCount & "," & lColumnCount
Msgbox "CellData: " & oChild(lCount).GetCellData(lRowCount , lColumnCount)
Next
Next
Next
oChild(0).ChildItem(2,2,"Image",0).Click
Set ōChild = Nothing
Set ōDesc = Nothing
Note that I've left some of the less interesting stuff out that opens the page, declares variables, etc. This will allow me to grab an array of all of the tables, interate through them, in turn looping thru each column and row. The click line works for my example HTML, but I need to tweak it to use the dynamic stuff above for the determination.
We are using the Descrīptive programming to get at our array of tables. Some interesting notes:
1) The tables array is zero-based
2) The rows/columns seem to be collections, and are therefore 1 based
3) The two tables in my example HTML are nested, but both show up in the tables array. I find this interesting, but very useful. Since it works that way, I don't have to loop a table's row/column combination to determine if a particular cell contains a nested table. Allows for a much more dynamic way to get at certain information.