使用VBA自动登录网站 - 获取“自动化错误”“未指定错误”2147467259(80004005)

时间:2014-01-09 01:58:36

标签: vba


我过去使用网站登录成功了,但由于某种原因,我试图自动化的最后两个网站根本没用。也许是因为它们是jhtml和aspx网站,我不知道。下面是aspx示例。希望与此相关的功能可以与另一个一起使用。

Sub ws8c()

Dim appIEc As SHDocVw.InternetExplorerMedium ' InternetExplorer
Set appIEc = New SHDocVw.InternetExplorer
appIEc.navigate "http://www.bentekenergy.com/Login.aspx"

While appIEc.Busy
    DoEvents
Wend

' I get the error here! if I comment this out I get the error here too
appIEc.document.getElementById("ctl00_MasterMainContent_LoginCtrl_Username").Value = "MyUserName"

'if I comment the above line out I get the error here too
appIEc.document.getElementById("ctl00_MasterMainContent_LoginCtrl_Password").Value = "MyPW"

' This part doesn't work either... 
Set ElementCol = appIEc.document.getElementsByTagName("a")
For Each btna In ElementCol
If btna.ID = "ctl00_MasterMainContent_LoginCtrl_btnSignIn" Then
btna.Click
Exit For
End If
Next btna

While appIEc.Busy
    DoEvents
Wend

appIEc.navigate "http://www.bentekenergy.com/Benport/HubFlowMaps.aspx?sg=6", CLng(2048)

Set appIEc = Nothing

End Sub

我尝试使用其他技术,如getelementsbytagname(“input”),然后循环浏览并查找上面显示的ID。我不得不承认我有点把其他人的帖子拉到一起,所以我可能会错过一些重要的东西。什么是疯了我在其他地方工作的代码非常相似......

1 个答案:

答案 0 :(得分:0)

您应该确定应用程序所需的安全设置(完整性级别)。

根据我的理解,Internet Explorer默认使用低完整性应用程序设置。

完整性设置在Windows 7的用户访问控制(UAC)中定义,UAC定义系统信任哪些程序以进行某些更改。

当您最初声明变量appIEC时,您将其声明为shDocView.InternetExplorerMedium,这表示您要使用具有中等完整性设置的对象(Internet Explorer)。

然而,当您在set语句中创建对象时,您使用默认的低完整性设置创建了它。

根据我的经验,设置为可信站点的网站在中等完整性下运行,但不会在低完整性下运行。同样,受信任的网站必须在中/高完整性下运行,否则会发生自动化错误。

希望这会有所帮助!!

相关问题