【发布时间】:2018-03-19 19:28:32
【问题描述】:
第一个网页抓取项目!
我一直在从这里复制各种网络抓取代码,但无法绕过
运行时错误 13:类型不匹配
在.document.getElementById("") 行上,我用来为要单击的超链接设置变量。我认为它应该被视为我成功编码的登录按钮。我不确定我是否缺少我应该使用的库,因为几乎所有其他帖子都有与我遇到的不同的问题和解决方案。我在这里做错了什么?
我正在使用 IE11 和 Excel 2010。我开始添加我认为可能会提供解决方案的库。我激活的库如下:
- Visual Basic 应用程序
- Microsoft Excel 14.0 对象库
- OLE 自动化
- Microsoft Office 14.0 对象库
- Microsoft HTML 对象库
- Microsoft Internet 控件
- 微软 XML,v6.0
- Microsoft Shell 控件和自动化
这里是代码和 HTML DOM sn-p:
Sub IEScrape()
'we define the essential variables
Dim ie As Object
Dim pwd, username
Dim button
Dim MemAss
'add the "Microsoft Internet Controls" reference in your VBA Project indirectly
Set ie = New InternetExplorerMedium
With ie
.Visible = True
.navigate ("internalwebsite.com")
While ie.readyState <> 4
DoEvents
Wend
Set username = .document.getElementById("userid") 'id of the username control (HTML Control)
Set pwd = .document.getElementById("password") 'id of the password control (HTML Control)
Set button = .document.getElementById("loginbtn") 'id of the button control (HTML Control)
username.Value = "username"
pwd.Value = "password"
button.Click
While ie.readyState <> 4
DoEvents
Wend
'Run time error 13: Type mismatch on next line!!!
Set MemAss = .document.getElementById("Menu:membershipassociation") 'id of the link (HTML Control)
MemAss.Click
While ie.readyState <> 4
DoEvents
Wend
End With
Set ie = Nothing
End Sub
【问题讨论】:
-
尝试只在同一行附加
.Click,而不设置变量(在这种情况下为MemAss)。.document.getElementById("Menu:membershipassociation").Click. -
@NicholasKemp 我收到运行时错误 424:需要对象
-
您是否使用所有这些参考资料?
-
@ashleedawg 绝对是其中一些。我会说最后两个,也许我不是 OLE 自动化,但不知道如何检查。我刚开始随意添加它们,看看它们是否有帮助。
-
我认为您可能没有在模块的最顶部使用
Microsoft HTML Object Library, Miicrosoft XML, v6.0, Microsoft Shell Controls And Automation. Also, make sure you haveOption Explicit`(总是)。这些东西不一定会帮助您找到 this 错误,但会有助于防止其他错误。对于这个问题,也许您可以使用getElementsByClassName来代替课程。您确定您在 HTML 的正确部分吗? (document.innerhtml的值是否与您期望的一样?)