【发布时间】:2017-04-05 00:41:57
【问题描述】:
我是 VBA 和一般编程的新手,我在网上找到了一些代码来自动化网页处理。现在我只是尝试登录 e-Oscar 的网页:
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now())
Loop
Set objCollection = IE.document.getElementsByTagName("input")
Set objName = IE.document.getElementsByTagName("name")
i = 0
While i < objCollection.Length
If objCollection(i).ID = "companyId" Then
' Set text for search
objCollection(i).Value = "companyId"
Else
If objCollection(i).ID = "userId" Then
objCollection(i).Value = "uId"
Else
If objCollection(i).ID = "password" Then
objCollection(i).Value = "pwd"
Else
If objCollection(i).ID = "securityMsgAck1" Then
objCollection(i).Value = True
'Else
'If objName(i).Name = "securityMsgAck" Then
'objName(i).Value = True
'Else
'If objCollection(i).ID = "button" Then
'objCollection(i).Value = True
' "Search" button is found
Set objElement = objCollection(i)
End If
End If
End If
End If
'End If
'End If
i = i + 1
Wend
objElement.Click ' click button to search
按原样运行时。这段代码打开了我试图自动化的网页,填写了公司 ID、用户 ID、密码,并且似乎选中了我确认该页面的 T&C 的框。但是,每当我取消注释“securityMsgAck”时,它不再选中该框并给我一个错误:
运行时错误“91”:对象变量或未设置块变量。\
如果我注释掉“securityMsgAck”并取消注释掉“button”,它也会这样做。似乎我的代码在这两个部分未执行时尝试检查该框,但是当它们是语句时,要么在尝试按下它之前失败,要么完全阻止它。我不太确定...任何建议将不胜感激。提前致谢。
编辑:每当我取消注释该代码的任一部分时,我的程序也将不再填写公司 ID、用户 ID 或密码,并且无法检查安全消息框...
【问题讨论】:
-
此页面上不存在ID“按钮”,您应该使用类“btn”
-
@h2so4 嗯,这很有道理,但我的程序至少“尝试”以现在的方式登录(我认为)。我知道这一点是因为我收到了来自网页的错误消息,我没有在尝试登录之前“检查标记”安全消息以确认它..
标签: vba excel internet-explorer input