Jump to content

Read Internet Explorer table by rows with link text


Recommended Posts

Hello,

I'm working on pulling some data from a table in Internet Explorer and writing the contents, on a row by row basis, into a csv file. . I am able to loop through to an extent but have the following questions:

1. How do I determine the end of a row

2. Sometimes the table data will have a hyperlink attached. I'd very much like to capture that url in my csv file.

If I use the _IETableWriteToArray function, I am able to do everything I need except for getting the URL. Does anyone know a way to pull the URL for certain cells in the table? I have looked at a For...Next using _IETagnameGetCollection but am not successful. Thanks in advance for any assistance! Code below:

$oTable = _IETableGetCollection($oIE,0)
    $oTRs = _IETagnameGetCollection($oTable, "TR")

    For $oTR In $oTRs
        $oTDs = _IETagnameGetCollection($oTR, "TD")
        For $oTD In $oTDs
            $sRowCont = _IEPropertyGet($oTD, "innertext")
            msgbox(0,"Cell Text:",$sRowCont)
            $oTD = _IETagnameGetCollection($oTR, "TD", 0)
            $oLink = _IETagnameGetCollection($oTD, "a", 0)
            msgbox(0,"Link Text",$oLink )
            ;ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $blnFound = ' & $blnFound & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
        Next
    Next
Link to comment
Share on other sites

I would personally just modify _IETableWriteToArray() to return .innerHTML instead of .innerText

Try the function below which is the _IETableWriteToArray function with "$a_TableCells[$col][$row] = $td.innerText" changed to "$a_TableCells[$col][$row] = $td.innerHTML".

Keep in mind, this will return all of the data in the <td> so it may return more than you want, but it's a start.

; #FUNCTION# ====================================================================================================================
; Name...........: _IETableWriteToArray
; Description ...: Reads the contents of a Table into an array
; Parameters ....: $o_object    - Object variable of an InternetExplorer.Application, Table object
;                   $f_transpose- Boolean value.  If True, swap rows and columns in output array
; Return values .: On Success     - Returns a 2-dimensional array containing the contents of the Table
;                  On Failure    - Returns 0 and sets @ERROR
;                    @ERROR        - 0 ($_IEStatus_Success) = No Error
;                                - 3 ($_IEStatus_InvalidDataType) = Invalid Data Type
;                                - 4 ($_IEStatus_InvalidObjectType) = Invalid Object Type
;                    @Extended    - Contains invalid parameter number
; Author ........: Dale Hohm
; ===============================================================================================================================
Func _IETableWriteToArrayHTML(ByRef $o_object, $f_transpose = False)
    If Not IsObj($o_object) Then
        __IEErrorNotify("Error", "_IETableWriteToArray", "$_IEStatus_InvalidDataType")
        Return SetError($_IEStatus_InvalidDataType, 1, 0)
    EndIf
    ;
    If Not __IEIsObjType($o_object, "table") Then
        __IEErrorNotify("Error", "_IETableWriteToArray", "$_IEStatus_InvalidObjectType")
        Return SetError($_IEStatus_InvalidObjectType, 1, 0)
    EndIf
    ;
    Local $i_cols = 0, $tds, $i_col
    Local $trs = $o_object.rows
    For $tr In $trs
        $tds = $tr.cells
        $i_col = 0
        For $td In $tds
            $i_col = $i_col + $td.colSpan
        Next
        If $i_col > $i_cols Then $i_cols = $i_col
    Next
    Local $i_rows = $trs.length
    Local $a_TableCells[$i_cols][$i_rows]
    Local $col, $row = 0
    For $tr In $trs
        $tds = $tr.cells
        $col = 0
        For $td In $tds
            $a_TableCells[$col][$row] = $td.innerHTML
            $col = $col + $td.colSpan
        Next
        $row = $row + 1
    Next
    If $f_transpose Then
        Local $i_d1 = UBound($a_TableCells, 1), $i_d2 = UBound($a_TableCells, 2), $aTmp[$i_d2][$i_d1]
        For $i = 0 To $i_d2 - 1
            For $j = 0 To $i_d1 - 1
                $aTmp[$i][$j] = $a_TableCells[$j][$i]
            Next
        Next
        $a_TableCells = $aTmp
    EndIf
    Return SetError($_IEStatus_Success, 0, $a_TableCells)
EndFunc   ;==>_IETableWriteToArray
Edited by danwilli
Link to comment
Share on other sites

  • 2 years later...

One of my signature links is good for this kind of thing.  You write an xpath, and it will return an array of matching objects.

edit: oops, posted on someone digging up a necro

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...