使用WebElement方法的SeleniumBasic VBA WebElement最快循环 [英] SeleniumBasic VBA Fastest Loop of WebElements using a WebElement method
问题描述
我注意到操作需要相当长的时间.
I'm noticing a rather long time for an operation to complete.
我正在使用最新的SeleniumBasic for VBA,以使用ChromeDriver从表中提取数据. ( https://github.com/florentbr/SeleniumBasic )
I'm using the latest SeleniumBasic for VBA to extract data from a table using a ChromeDriver. (https://github.com/florentbr/SeleniumBasic)
我正在检索WebElement并遍历它们以获取文本值.
I'm retrieving WebElements and looping through them to get the text value.
我正在将文本值分配给String类型的数组.
I'm assigning the text value to an array of type String.
当我有一个大数组(1000个WebElement对象)时,此操作将花费很长时间.
This operation takes quite a long time when I have a large array (1000's of WebElement objects).
问题-获取所有文本值的最快方法是什么?
这是我的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多个表格行.
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代码
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
推荐答案
进行了更多研究之后,可以使用名为TableElement的对象. 这几乎立即提取HTML表并将其转储到二维VBA数组中.
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
@florentbr的积分-
Credits to @florentbr -
https://github.com/florentbr/SeleniumBasic/issues/33 #issuecomment-153500008
这篇关于使用WebElement方法的SeleniumBasic VBA WebElement最快循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!