对不起伙计们,这更像是上一个问题的更新问题...我设法找出了我在IE中单击“下一个结果”按钮并将其循环回来拉出的问题。相同的数据,然后单击“下一个结果”按钮并拉出相同的数据...依此类推,直到......这里是我遇到问题的地方。我在“Loop Until”语句中不断出现“需要对象”错误...我尝试了许多不同的解决方法,切换到变量等,但仍然遇到错误。基本上我只想在“下一个结果”按钮不再可用时停止循环(当没有更多数据可以拉动时,这显然会发生)。
以下是造成问题的VBA部分......
Dim TDelements As IHTMLElementCollection
Dim TDelement As HTMLTableCell
Dim r As Long
Set TDelements = IE.document.getElementsByTagName("tr")
r = 0
Do
Application.Wait Now + TimeValue("00:00:03")
For Each TDelement In TDelements
If TDelement.className = "searchActivityResultsContent" Then
Sheet1.Range("E1").Offset(r, 0).Value = TDelement.ChildNodes(8).innerText
r = r + 1
ElseIf TDelement.className = "searchActivityResultsContent" Then
Sheet1.Range("E1").Offset(r, 0).Value = TDelement.ChildNodes(8).innerText
r = r + 1
End If
Next
Application.Wait Now + TimeValue("00:00:02")
Set elems = IE.document.getElementsByTagName("input")
For Each e In elems
If e.Value = "Next Results" Then
e.Click
Exit For
End If
Next e
Loop Until e.Value <> "Next Results"
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
IE.Quit
这是我所指的按钮的HTML代码......
<table class="contentTable" align="center">
<tr class="contentTableTR">
<form name="scrollResultListForm" method="post" action="/scrollTransactionsList.do" onsubmit="return isBusy();">
<input type="submit" name="action" value="Next Results" onmouseover="showComment(event,'Display next results')" onmouseout="hideComment()" class="formButton">
<input type="submit" name="action" value="Last Page" onmouseover="showComment(event,'Goto last result page')" onmouseout="hideComment()" class="formButton">
</form>
</tr>
</table>
答案 0 :(得分:0)
第一个问题:&#34; last&#34;是否有IE.document.getElementsByTagName("input")
页? (我假设这个。但我想确定)
尝试稍微更改一下代码:
....
Set elems = IE.document.getElementsByTagName("input")
Dim Next_Loop As Boolean
Next_Loop = True
For Each e In elems
If e.Value = "Next Results" Then
e.Click
Next_Loop = False
Exit For
End If
Next e
Loop Until Next_Loop
....
如果For Each
循环结束,变量将为空,这会导致错误。或者,您只需将Loop Until e.Value <> "Next Results"
行更改为:
Loop Until e Is Nothing
但只有在e
被声明为对象时才有效;)
修改强>
请试试这个:
Dim TDelements As IHTMLElementCollection
Dim TDelement As HTMLTableCell
Dim r As Long, i As Long
Dim e As Object
Set TDelements = IE.document.getElementsByTagName("tr")
r = 0
For i = 1 To 1
Application.Wait Now + TimeValue("00:00:05")
For Each TDelement In TDelements
If TDelement.className = "searchActivityResultsContent" Then
Sheet1.Range("E1").Offset(r, 0).Value = TDelement.ChildNodes(8).innerText
r = r + 1
ElseIf TDelement.className = "searchActivityResultsContent" Then
Sheet1.Range("E1").Offset(r, 0).Value = TDelement.ChildNodes(8).innerText
r = r + 1
End If
Next
Application.Wait Now + TimeValue("00:00:05")
Set elems = IE.document.getElementsByTagName("input")
For Each e In elems
If e.Value = "Next Results" Then
e.Click
i = 0
Exit For
End If
Next e
Next i
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
IE.Quit
答案 1 :(得分:0)
当所有for each
用尽时,e
结束。因此,当您到达e
Nothing
必须为Loop