我对VBA比较陌生,我正在尝试整理一个msgbox,它会从网页中获取一个特定的数字,但是我一直遇到运行时错误'91',我根本无法弄清楚如何解决这个问题。我搜索了无数的stackoverflow问题,youtube视频和一般的谷歌搜索,但是我没有成功找到错误。
以下是代码:
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("http://brokercheck.finra.org")
Do
DoEvents
Loop Until IE.ReadyState = 4
'Enter values from the corresponding sheet
'Set some generic typing for ease
Set doc = IE.document
doc.GetElementbyID("GenericSearch_IndividualSearchText").Value = Worksheets("Master").Range("D203")
doc.GetElementbyID("GenericSearch_EmploymingFirmSearchText").Value = Worksheets("Master").Range("C203")
Set elements = doc.getElementsByTagName("button")
For Each element In elements
If element.getAttribute("type") = "submit" Then
element.Click
Exit For
End If
Next element
Do
DoEvents
Loop Until IE.ReadyState = 4
'find CRD#
Set crd = doc.getElementsByClassName("summarydisplaycrd")(0).innerText 'here is where the run time error occurs
MsgBox crd
和我试图从以下信息获取HTML:
<div class="searchresulttext">
<div class="bcrow">
<div class=""> <span class="summarydisplayname">[redacted]</span> <span class="summarydisplaycrd text-nowrap">(CRD# 5944070)</span></div>
答案 0 :(得分:1)
我正在审核此代码和finra.org网站,并提出以下意见,一经解决,应解决问题。
根据“检查”按钮返回的实际HTML,您提供的HTML示例完全不正确。
返回的实际HTML如下所示,类名为"displayname"
,而不是"summarydisplaycrd"
:
<div class="SearchResultItemColor bcrow">
<div class="searchresulttext">
<div class="bcsearchresultfirstcol">
<span class="displayname">[redacted]</span> <span class="displaycrd">(CRD# 123456789)</span>
For each element
循环。这个可能不是“检查”按钮(虽然我可以获得结果,但您可能希望在代码中添加更多逻辑以确保提交“检查”按钮。<强>更新强>
在进一步审查时,虽然我可以复制91型错误,但我仍然不知道为什么你的班级名称与我的不同(可能是IE11的东西,不知道......)无论如何,我能够通过强制更长的延迟来解决这个问题,因为在这种情况下DoEvents
循环根本就不够用(有时这是从外部函数动态提供数据的情况,浏览器是ReadyState = 4和.Busy = True,所以循环没有做任何事情)
我使用WinAPI睡眠功能并在按下“点击”按钮后强制延迟1秒,在ReadyState = 4
和 .Busy=True
的条件下循环。
注意您需要修改classname参数,具体取决于它在HTML上的显示方式。
Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub finra()
Dim IE As Object
Dim doc As Object, element As Object, elements As Object, crd
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("http://brokercheck.finra.org")
Call WaitIE(IE, 1000)
'Enter values from the corresponding sheet
'Set some generic typing for ease
Set doc = IE.document
doc.GetElementbyID("GenericSearch_IndividualSearchText").Value = "steve"
doc.GetElementbyID("GenericSearch_EmploymingFirmSearchText").Value = "ed"
Set elements = doc.getElementsByTagName("button")
For Each element In elements
If element.getAttribute("type") = "submit" Then
If element.innerText = "Check " Then
element.Click
Exit For
End If
End If
Next element
Call WaitIE(IE, 1000)
Dim itms As Object
'Set itms = doc.getElementsByClassName("displaycrd")
crd = doc.getElementsByClassName("displaycrd")(0).innerText 'here is where the run time error occurs
MsgBox crd
End Sub
Sub WaitIE(IE As Object, Optional time As Long = 250)
Dim i As Long
Do
Sleep time
Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.ReadyState = 4) & _
vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
i = i + 1
Loop Until IE.ReadyState = 4 And Not IE.Busy
End Sub