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

问题描述 投票:0回答:1

我注意到需要较长时间才能完成操作。

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

我正在检索WebElement并遍历它们以获取文本值。

我正在将文本值分配给String类型的数组。

当我有一个大数组(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代码

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
excel vba selenium selenium-webdriver selenium-chromedriver
1个回答
0
投票

经过更多研究后,可以使用名为TableElement的对象。这几乎立即提取HTML表并将其转储到二维VBA数组中。

'Credits to @florentbr 
Private Sub Iterate_A_Table2()
Dim driver As New FirefoxDriver, Assert As New Assert
driver.Get "http://the-internet.herokuapp.com/tables"

Dim tbl As TableElement
Set tbl = driver.FindElementByCss("#table1").AsTable

Dim Data()
Data = tbl.Data
For c = 1 To UBound(Data, 1)
  For r = 1 To UBound(Data, 1)
    Debug.Print Data(r, c)
  Next
  Debug.Print Empty
Next

driver.Quit
End Sub

归功于@florentbr-

https://github.com/florentbr/SeleniumBasic/issues/33#issuecomment-153500008

© www.soinside.com 2019 - 2024. All rights reserved.