问题
I'm noticing a rather long time for an operation to complete.
I'm using the latest SeleniumBasic for VBA to extract data from a table using a ChromeDriver. (https://github.com/florentbr/SeleniumBasic)
I'm retrieving WebElements and looping through them to get the text value.
I'm assigning the text value to an array of type String.
This operation takes quite a long time when I have a large array (1000's of WebElement objects).
Question - What is the fastest way to get all the text values?
Here is my 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>
Each table row has 6 data points, specified by the td tag. I've cut the snippet to only 1 table row but just imagine having 100+ table rows.
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
回答1:
After doing some more research, there is an object called TableElement which can be used. This extracts HTML tables almost instantly and dumps it in a two dimensional VBA array.
'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
Credits to @florentbr -
https://github.com/florentbr/SeleniumBasic/issues/33#issuecomment-153500008
来源:https://stackoverflow.com/questions/61028387/seleniumbasic-vba-fastest-loop-of-webelements-using-a-webelement-method