找回密码
 加入
搜索
查看: 777|回复: 3

[IE类操作] 【已解决】求各位前辈赐教关于_HtmlTableGetWriteToArray函数

[复制链接]
发表于 2022-6-27 14:37:21 | 显示全部楼层 |阅读模式
本帖最后由 laoxgogo 于 2022-6-30 17:29 编辑

Func _WD_GetTable($sSession, $sBaseElement)
Local Const $sFuncName = “_WD_GetTable”
Local $aElements, $sElement, $iLines, $iRow, $iColumns, $iColumn, $sHTML
$_WD_HTTPRESULT = 0
$_WD_HTTPRESPONSE = ''

;确定可选 UDF 是否可用
Call(“_HtmlTableGetWriteToArray”, “”)

如果 @error = 0xDEAD 并且 @extended = 0xBEEF 则
$aElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sBaseElement & “/tbody/tr”, “”, True) ;检索表行
数 如果 @error <> $_WD_ERROR_Success 则返回 SetError(__WD_Error($sFuncName, @error), 0, “”)

$iLines = UBound($aElements)
$aElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sBaseElement & “/tbody/tr[1]/td”, “”, True) ;通过检查第一个表行
来检索表列数 如果 @error <> $_WD_ERROR_Success 则返回 SetError(__WD_Error($sFuncName, @error), 0, “”)

$iColumns = UBound($aElements)
Local $aTable[$iLines][$iColumns] ;创建 AutoIt 数组以保存表
的所有单元格 $aElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sBaseElement & “/tbody/tr/td”, “”, True) ;检索所有表单元格
如果 @error <> $_WD_ERROR_Success 则返回 SetError(__WD_Error($sFuncName, @error), 0, “”)

for $i = 0 To UBound($aElements) - 1
$iRow = Int($i / $iColumns) ;计算 AutoIt 数组中存储单元格值
的行/列 $iColumn = Mod($i, $iColumns)
$aTable[$iRow][$iColumn] = _WD_ElementAction($sSession, $aElements[$i], “Text”) ;检索每个表单元格
的文本 如果 @error <> $_WD_ERROR_Success 则返回 SetError(__WD_Error($sFuncName, @error), 0, “”)

下一页
Else
;获取 table 元素
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sBaseElement)
如果 @error <> $_WD_ERROR_Success 则返回 SetError(__WD_Error($sFuncName, @error), 0, “”)

;检索其 HTML
$sHTML = _WD_ElementAction($sSession, $sElement, “Property”, “outerHTML”)
如果 @error <> $_WD_ERROR_Success 则返回 SetError(__WD_Error($sFuncName, @error), 0, “”)

;Convert to array
$aTable = _HtmlTableGetWriteToArray($sHTML, 1, False, $_WD_IFILTER)
EndIf

Return $aTable
EndFunc ;==>_WD_GetTable



关于WebDriver-0.9.1 wd_helper.au3 中的_HtmlTableGetWriteToArray函数哪来的?论坛查不到,求各位前辈赐教!感激不尽!!!
 楼主| 发表于 2022-6-27 14:43:46 | 显示全部楼层
之前想查查_WD_GetTable这个函数怎么用,然后老是读不出来,去官网论坛看看发现了下面的那个函数,能读但是很慢,都还不如ie的_IETableWriteToArray


Func _WD_GetTable_2($sSession, $sTableXpath)

;~     Const $sFuncName = "_WD_GetTable_2"

    Local $aTable[0], $aRowElements[0], $aColElements[0]
    Local $sRowColumns = ""
    Local $iRows = 0, $iCols = 0, $iChildCount = 0

    ;Get table row elements
    $aRowElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sTableXpath & "/tbody/tr", "", True)
    If @error <> $_WD_ERROR_Success Then Return "cw"
    $iRows = UBound($aRowElements)

    ;Find max number of columns by checking each row's child count property
    For $iRow = 0 To UBound($aRowElements) - 1
        $iChildCount = _WD_ElementAction($sSession, $aRowElements[$iRow], "property", "childElementCount")
        If @error <> $_WD_ERROR_Success Then Return "cw"

        ;If number of row columns is greater the previous value, then save new value
        If $iChildCount > $iCols Then $iCols = $iChildCount
    Next
    ;Dimension and load table data
    ReDim $aTable[0][$iCols]

    For $iRow = 0 To $iRows - 1
        ;Get column elements (td or th)
        $aColElements = _WD_FindElement($sSession, _
                                        $_WD_LOCATOR_ByXPath, _
                                        "./td|./th", _
                                        $aRowElements[$iRow], _
                                        True)
        If @error <> $_WD_ERROR_Success Then Return "cw"

        ;Build pipe-separated row of column data
        $sRowColumns = ""
        For $iCol = 0 To UBound($aColElements) - 1
            If $iCol = 0 Then
                $sRowColumns &= _WD_ElementAction($sSession, $aColElements[$iCol], "text")
            Else
                $sRowColumns &= "|" & _WD_ElementAction($sSession, $aColElements[$iCol], "text")
            EndIf
        Next

        ;Add row to array
        _ArrayAdd($aTable, $sRowColumns)
    Next

    ;Return result table
    Return $aTable

EndFunc
发表于 2022-6-27 14:53:12 | 显示全部楼层
 楼主| 发表于 2022-6-27 15:07:30 | 显示全部楼层
redapple2008 发表于 2022-6-27 14:53
https://www.autoitscript.com/forum/topic/167679-read-data-from-html-tables-from-raw-html-source/

谢谢前辈指点!!!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-3-29 01:58 , Processed in 0.068964 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表