Excel VBA配合Selenium网页抓取时FindElementsBy元素查找报错求助
代码问题排查与修复方案
核心报错原因
ProcessTable过程内定义的TableToProcess变量未被赋值,属于空对象,直接调用FindElementsByTag方法必然触发运行时错误。- 调用
ch.Get加载页面后未等待DOM元素渲染完成,直接执行元素查找逻辑大概率会出现元素不存在的报错。 - 主过程未将页面中的表格对象传递给
ProcessTable,子过程无法获取到需要解析的页面内容。
修正后完整代码
Option Explicit Private ch As Selenium.ChromeDriver Sub ScrapeEntriesTokyo() Dim FindBy As New Selenium.By Dim TargetTable As Selenium.WebElement Set ch = New Selenium.ChromeDriver ch.Start ch.Get "https://www.worldathletics.org/competitions/olympic-games/the-xxxii-olympic-games-athletics-7132391/athletes" ' 等待页面基础渲染 ch.Wait 1000 ' 定位页面表格,最多等待10秒加载 Set TargetTable = ch.FindElementByCss("table", 10000) ' 将定位到的表格传递给处理过程 Call ProcessTable(TargetTable) ' 爬取完成后关闭浏览器释放资源 ch.Quit Set ch = Nothing End Sub ' 增加参数接收传递过来的表格对象 Sub ProcessTable(TableToProcess As Selenium.WebElement) Dim AllRows As Selenium.WebElements Dim SingleRow As Selenium.WebElement Dim AllRowCells As Selenium.WebElements Dim SingleCell As Selenium.WebElement Dim OutputSheet As Worksheet Dim RowNum As Long, ColNum As Long Dim TargetCell As Range Application.ScreenUpdating = False Set OutputSheet = ThisWorkbook.Worksheets.Add Set AllRows = TableToProcess.FindElementsByTag("tr") For Each SingleRow In AllRows RowNum = RowNum + 1 Set AllRowCells = SingleRow.FindElementsByTag("td") If AllRowCells.Count = 0 Then Set AllRowCells = SingleRow.FindElementsByTag("th") End If For Each SingleCell In AllRowCells ColNum = ColNum + 1 Set TargetCell = OutputSheet.Cells(RowNum, ColNum) TargetCell.Value = SingleCell.Text Next SingleCell ColNum = 0 Next SingleRow OutputSheet.Range("A1").CurrentRegion.EntireColumn.AutoFit OutputSheet.ListObjects.Add Source:=OutputSheet.Range("A1").CurrentRegion Application.ScreenUpdating = True End Sub
额外说明
如果仍出现元素查找失败的问题,可调整FindElementByCss的选择器,直接指定页面中表格的class或者ID属性进行定位,匹配精度更高。
内容的提问来源于stack exchange,提问作者CarlosFC
相关产品推荐
相关产品推荐

