Created On:  24 February 2012
 
How can we capture the text property of a HMTLDiv object with multiple nested Divs, the HTMLDiv is non-scrollable.  HTMLDiv ("name").CaptureText only captures the current position.  Assuming the HTMLDiv contains 100+ nested Divs, we have to scroll to certain positions to be able to CaptureText using CaptureText(Left, Top, Width, Height).  Calling CaptureText at the HTMLDiv parent level with scroll options automatically seems to position the page at the top of the HTMLDiv.  Since the HTMLDiv is non-scrollable, the scroll option does not get handled.  We have to scroll at the browser level to capture the text, but this appears to capture data that is not part of the HTMLDiv.


For example, the following does not return any data:

Sub Main()
    IEWindow("Application=IEXPLORE.EXE Caption='Test Page - Windows Internet Explorer'").Attach

    ' Attach to Caption='Test page' Index=2
    HTMLBrowser("Caption='Test Page' Index=2").Attach
   
    Dim divObj As THTMLDiv
    Set divObj = HTMLDiv("ID=HTML_REPEATER_LiteServiceCatalogSearch")
   
    ' returns empty list
    MsgBox divObj.CaptureText(1, 1, 2000, 2500), vbOKOnly
   
    ' returns zero items
    MsgBox divObj.Children.Count, vbOKOnly, "Number of Child Objects"
End Sub

 
The innerText property of an HTMLDiv object contains the text of all the nested HTMLDivs including the parent, therefore, we can create a function to capture the innerText property.  The function will also utilise the Split method to split the captured string using a delimeter.   The result is that the split string is then stored to a collection representing the same information that we expected to capture from the HTMLDiv object.  See solution below containing the function getItems which takes a TObject and a string delimiter as argument and returns an ordered set of items as a single Collection object.


Sub Main()
   
    IEWindow("Application=IEXPLORE.EXE Caption='Test Page - Windows Internet Explorer'").Attach

    HTMLBrowser("Caption='Test Page' Index=2").Attach
   
    ' htmlDiv object
    Dim divObj As THTMLDiv
    Set divObj = HTMLDiv("ID=HTML_REPEATER_LiteServiceCatalogSearch")
   
    ' ordered set
    Dim cItems As New Collection
    Set cItems = getItems(divObj, "Qty")
   
    ' using the set we can manipulate the returned data
    Dim i As Integer
    For i = 1 To cItems.Count
        MsgBox cItems.Item(i), vbOKOnly, "Items"
    Next
       
End Sub

' function to get the text property of nested HTMLDiv
' data returned in an ordered set of items as a single Collection object

Public Function getItems(ByVal obj As TObject, ByVal delimeter As String) As Collection
    Dim CapturedText As String
    CapturedText = obj.InnerText

    Dim cItems As New Collection
    Dim StrSplit As Variant
    Dim nItems As Integer
   
    StrSplit = Split(CapturedText, delimeter)
    nItems = UBound(StrSplit) - LBound(StrSplit) - 1
   
    Dim i As Integer
    For i = 0 To nItems
        cItems.Add (StrSplit(i))
    Next
   
    Set getItems = cItems
End Function


Note: an example nested HTMLDiv sample page is attached to the article for demonstrative purposes.