提问人:PJB 提问时间:6/25/2023 最后编辑:PJB 更新时间:6/26/2023 访问量:71
VBA Web Scraper For 循环从 Excel 列 C 输入数据,给出运行时错误“438”:对象不支持此属性或方法
VBA Web Scraper For Loop to Input Data from Excel Column C is Giving Run-time error '438': Object Doesn't Support This Property or Method
问:
我对 VBA 非常陌生,因为这是一台工作计算机,我不能使用 Selenium 之类的东西。我的宏的目的是为我的老板从基于供应商的应用程序(数据存储在我公司的服务器上)生成工作报告。我已经删除了很多代码,因为有些代码包含机密信息。
我正在尝试使用 For 循环在搜索框中输入 ID,以将我带到我需要从中抓取数据的页面。这些 ID 列在名为“Data2”的工作表的 C 列中。我基本上需要它进入搜索框,然后点击一个按钮。
我的VBA:
Sub D_Scrape()
'Declare variables
Dim url As String
Dim browser As InternetExplorerMedium
Dim htmlDoc As Object
Dim cCoEnter As Object
Dim sButton As Object
Dim ws As Worksheet
Dim i As Long
Dim lRow As Long
Dim td As Object
Dim tr As Object
'Initialize variables
url = 'unable to share this
Set ws = Worksheets("Data2")
'Initialize IE & Set Visibility
Set browser = New InternetExplorerMedium
browser.Visible = True
browser.navigate url
'Set document referencefi
Set htmlDoc = browser.document
'Enter Collection into Search box in collection
Application.Wait (Now + TimeSerial(0, 0, 8))
lRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
Set cCoEnter = htmlDoc.getElementsByName("collectionSearchBox:collectionSearchTxt")
For i = 2 To lRow
cCoEnter.Value = ws.Range("C" & i).Value
htmlDoc.getElementsByName("collectionSearchBox:collectionSearchBtn").Item.Click
'Get Mileage
Set tr = htmlDoc
Application.Wait (Now + TimeSerial(0, 0, 1))
For each td in tr.getElementsByName("collectionInfoBox;SummaryInformationBox:milesTraveled")
ws.Range("K" & i).Value = td.getAttribute("value")
Next
Next i
End Sub
适用于此的 HTML 是:
<tr align="left">
<td align="left" width="15%"><label>ID:</label></td>
<td align="left" width="15%" name="collectionSearchBox:collectionSearchTxt" type="text" value=""/></td>
<td align="left"><input name="collectionSearchBox:collectionSearchBtn"/></td>
</tr>
我的Excel:
我看过几篇关于如何指定动态范围的帖子(有些月份我需要生成数百个报告,而其他月份则少得多)。
我收到运行时错误“438”:对象不支持此属性或方法cCoEnter.Value = ws.Range("C" & i).Value
我尝试将变量设置为 的范围,但它没有区别,仍然在 .ws.Range("C1:C" & lRow)
cCoEnter.Value = ws.Range("C" & i).Value
在“ID”框中未输入任何值。
我非常不确定还有什么可以尝试的,并希望有任何建议。先谢谢你。
答: 暂无答案
评论
ws.Range("C"&i).Value
cCoEnter
cCoEnter.Item.innerText = ws.Range("C" & i).Value