Selenium VBA多同类名表格爬取错误及陈旧元素引用问题求助
Selenium VBA爬取表格问题:错爬重复class表格+Stale Element引用错误
问题描述
- 原有Selenium VBA代码可正常爬取网页表格,网页更新后页面出现多个class名称为
.table的表格,导致代码爬取到错误的表格数据 - 同时出现错误提示:
stale element reference element is not attached to the page document - 需求:仅爬取第一个目标表格,排除第二个表格
当前使用代码
Option Explicit Sub Testing() Dim Dr As New Selenium.ChromeDriver Dim hTable, hBody, hTR, hTD, tb As Object Dim bb, tr, td As Object Dim r, c, lr1 As Long Dr.Get "url" Dr.Wait (5000) With Sheet1 .Activate Set hTable = Dr.FindElementsByCss(".table") For Each tb In hTable Set hBody = tb.FindElementsByTag("tbody") For Each bb In hBody Set hTR = bb.FindElementsByTag("tr") For r = 1 To hTR.Count - 1 Set hTD = hTR(r).FindElementsByTag("td") If hTD.Count = 0 Then Set hTD = hTD(r).FindElementsByTag("th") lr1 = .Cells(Rows.Count, 1).End(xlUp).Row + 1 For c = 2 To hTD.Count + 1 .Cells(lr1, c).Value = hTD(c - 1).Text Next c Next r End If Next bb Exit For Next tb End With Dr.Quit End Sub
补充材料
附上目标表格、需排除表格的HTML截图及补充截图,恳请技术支持。
内容的提问来源于stack exchange,提问作者TCritical
相关产品推荐
相关产品推荐

