我正在尝试使用 objElement 和 objCollection 从网站中提取数据。相关和工作代码如下:
Sub Step6()
Dim LoanNumX As Long
Dim LastX As String
Dim Phone1X As String
Dim Phone2X As String
Dim PiX As String
Dim TiX As String
Dim myText2 As String
ActiveWorkbook.Save
Range("M2").Select
Do While ActiveCell.Value <> ""
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
LastX = ActiveCell.Value
ActiveCell.Offset(, -12).Select
BigX = ActiveCell.Value
Phone1X = "N/A"
Phone2X = "N/A"
PiX = "N/A"
TiX = "N/A"
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://website.com"
Application.StatusBar = "https://website.com is loading. Please wait..."
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set objCollection = IE.Document.getElementsbyTagName("input")
A1 = 0
While A1 < objCollection.Length
If objCollection(A1).Name = "networkId" Then
objCollection(A1).Value = "ID"
Else
If objCollection(A1).Name = "password" Then
objCollection(A1).Value = "PW"
Else
If objCollection(A1).Name = "btnLogin" Then
Set objElement = objCollection(A1)
objElement.Click
End If
End If
End If
A1 = A1 + 1
Wend
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
上面的代码功能齐全。下面的代码直接出现在上面的代码之后,并且出现了问题:
Application.StatusBar = "Search form submission. Please wait..."
Set objCollection = IE.Document.getElementsbyTagName("input")
L = 0
While L < objCollection.Length
If objCollection(L).Name = "account" Then
objCollection(L).Value = BigX
Else
If objCollection(L).Name = "lastName" Then
objCollection(L).Value = LastX
Else
If objCollection(L).Type = "image" Then
Set objElement = objCollection(L)
objElement.Click
End If
End If
End If
L = L + 1
Wend
由于某种原因,上面的代码被跳过了。我不明白为什么,但是宏从不通过 while 语句——就好像 L > objCollection.Length 从一开始就一样。如果我进入脚本,它会从以下位置跳转:
While L < objCollection.Length
到以下代码段:
Wend
结果是打开 IE,访问初始页面,输入并提交登录数据,显示结果页面,然后打开另一个 IE 实例并重复循环。IE,初始页面,登录数据,结果页面,IE,初始页面,登录数据,结果页面,IE ......所以当我强制宏退出时,我最终会遇到许多实例都卡在同一页面上。
我已经尝试了几种不同的方法,但到目前为止一直无法解决问题。
这是我在 HTML 中追求的数据:
<td><input type="text" name="account" maxlength="9" size="25" value="" onkeypress="accountSelected()" class="inputbox"></td>
<td><input type="text" name="lastName" size="25" value="" onkeypress="lastNameSelected()" class="inputbox"></td>
<input type="image" src="images/search.gif" alt="Search">
任何帮助是极大的赞赏!另外,我使用的是 MS Excel 2007 和 IE 7。我知道 IE 7 不支持 IE.Document.getElementsbyClassName,因此排除了它作为潜在解决方案的可能性。我知道 Excel 2007 和 IE 7 都已过时,但我无法更新它们。