SeleniumBasic VBA 使用 WebElement 方法最快的 WebElement 循环

2024-02-01

我注意到完成操作需要相当长的时间。

我正在使用最新的 SeleniumBasic for VBA 使用 ChromeDriver 从表中提取数据。 (https://github.com/florentbr/SeleniumBasic https://github.com/florentbr/SeleniumBasic)

我正在检索 WebElements 并循环它们以获取文本值。

我将文本值分配给字符串类型的数组。

当我有一个大数组(1000 个 WebElement 对象)时,此操作需要相当长的时间。

问题 - 获取所有文本值的最快方法是什么?

这是我的 HTML

<table class="Tables_Table_0">
    <caption></caption>
    <thead class="thead-inverse">
        <tr>
            <th class="col-md-4">
                Name
            </th>
            <th class="text-center">
                Time
            </th>
            <th class="text-center">
                Number
            </th>
            <th class="text-center">
                Rate
            </th>
            <th class="text-center">
                Other
            </th>
            <th class="text-center">
                Final
            </th>

        </tr>
    </thead>

    <tbody>
        <tr class="SOME CLASS">
            <td>
                Name Here</a>
            </td>
            <td class="text-center">
                123.000
            </td>
            <td class="text-center">
                5
            </td>
            <td class="text-center">
                8%
            </td>
            <td class="text-center">
                20
            </td>
            <td class="text-center">
                300.00
            </td>
        </tr>
    </tbody>
</table>

每个表行有 6 个数据点,由 td 标记指定。我已将代码片段剪切为仅 1 个表行,但想象一下有 100 多个表行。

VBA Code

Dim table As WebElement, tableElements As WebElements, tableData() As String, Element
Dim tableIndex As Integer, tableDataCount As Integer

'Get the table
Set table = bot.FindElementByXPath("//*[@id=""Tables_Table_0""]")

'Get the <td> elements
Set tableElements = table.FindElementsByTag("td")

'Assign array size to variable to use later on during loops
tableDataCount = tableElements.Count

'Assign array size            
ReDim tableData(tableDataCount)

'Loop index counter       
tableIndex = 1

'PROBLEM HERE - TAKES TOO LONG WHEN I HAVE A BUNCH OF ROWS IN MY TABLE
'Loop each element and get the Text value            
For Each Element In tableElements
    tableData(tableIndex) = Element.text '
    tableIndex = tableIndex + 1
Next Element

SeleniumBasic 还有一个 .ToExcel 方法,使这变得更加容易:

Set tbl = bot.FindElementByCss("#table1").AsTable
tbl.ToExcel Range("A1")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SeleniumBasic VBA 使用 WebElement 方法最快的 WebElement 循环 的相关文章

随机推荐