Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

Loop Through TD Element in HTML document from inside Frame VBA Excel IE8 Automation

I am trying to capture Table data inside a particular webpage Frame using Excel VBA.

Unfortunately the website is non-public, so I cannot give access.

Here is a snippet from the END of HTML (showing the LAST cell data) I extracted from Firefox Debugger from the Table I need.

<td class="ewrcc s35 b-17"><div class="ewrcv-nw">ExampleValue</div></td>
</tr></tbody></table>

I have edited my thread post as @Tim Williams kindly pointed my in the right direction to first set a reference to the Frame inside the webpage (Frame 12)

After setting a reference, I have found all of the Table data using:

Set HTMLDoc = ie.document.frames(12).document
Set HTMLTD = HTMLDoc.getElementsByTagName("TD")

For xTD = 0 to HTMLTD.length - 1  

j = Len(HTMLDoc.getElementsByTagName("TD")(xTD).innertext)   

    If j > 0 and j < 50 then

    '''Code here

    debug.print HTMLDoc.getElementsByTagName("TD")(xTD).className
    debug.print HTMLDoc.getElementsByTagName("TD")(xTD).Innertext

    End If

Next xTD

I have produced a spreadsheet and can capture the data in a somewhat cumbersome way by using the Innertext from a static known string ("Oct") and then cycling a count through the number of columns known for each table I am retreiving (2 in this case)

Usually I loop through the Row/Cell .length, but I have found when looping through the TD elements they do not have ROW/CELL objects from the method I am using. I would much prefer this way as it appeared faster, and I could automate the task without prompting a variable to trigger where the table populates.

My question is now: Is there anything I am overlooking? Eg. Can I access the table through the Reference Frame 12 and cycle through Children / Parent etc.? I would really like to reference the Table Object to cycle through the rows & cells if possible

Here is a larger snippet of the last row of a Table, the values I need to extract are labelled "DATA VALUE 1-21":

</tr>
<tr class="r46" style="height:15pt;">
<td class="ewrcc s4 b-33"></td>
<td class="ewrcc s23 b-21"><div class="ewrcv-nwl">DATA VALUE 1/21</div>
</td><td class="ewrcc s24 b-21"></td>
<td class="ewrcc s24 b-21"></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 2/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 3/21</div></td>
<td colspan="3" class="ewrcc s23 b-35">
<div class="ewrcv-nwl">DATA VALUE 4/21</div></td>
<td ewrcolumnindex="9" class="ewrcc s24 b-21">
<div class="ewrcv-nw">DATA VALUE 5/21</div></td>
<td class="ewrcc s23 b-24"><div class="ewrcv-nwl">DATA VALUE 6/21</div></td>
<td class="ewrcc s23 b-21"></td>
<td class="ewrcc s23 b-21"></td><td colspan="2" class="ewrcc s23 b-35">
<div class="ewrcv-nwl">DATA VALUE 7/21</div></td>
<td ewrcolumnindex="15" class="ewrcc s34 b-21">
<div class="ewrcv-nw">DATA VALUE 8/21</div></td>
<td class="ewrcc s25 b-24"><div class="ewrcv-nw">DATA VALUE 9/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 10/21</div></td>
<td class="ewrcc s24 b-24"><div class="ewrcv-nw">DATA VALUE 11/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 12/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 13/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 14/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 15/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 16/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 17/21</div></td>
<td class="ewrcc s24 b-24"><div class="ewrcv-nw">DATA VALUE 18/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 19/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 20/21</div>
</td><td class="ewrcc s33 b-35">
<div class="ewrcv-nw">DATA VALUE21/21</div>/td>
</tr>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How many tables are there in that frame?

Try something like:

Set HTMLDoc = ie.document.frames(12).document
Set tbls = HTMLDoc.getElementsByTagName("table")

For x = 0 to tbls.length - 1  
    Set tbl = tbls(x)
    debug.print "Table# " & (x+1), "rows=" & tbl.Rows.length
Next x

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...