Detect when a web page is loaded without using sleep
以下代码来自上述链接的“UPD2:”部分中的代码,但不起作用。 objIE.getProperty
没有返回任何内容,因此它永远不会匹配第一个标记化实例strSignature
。我能够确定For Each
语句没有找到打开的浏览器实例。你能帮我解决一下代码吗?
我在使用IE9的Windows 7上。我的公司很快将升级到IE11。我认为这没有任何区别,因为我能够验证初始标记化strSignature
是否正常工作并被保留。它只是找不到打开的浏览器窗口,因此无法获取第二个令牌进行比较。
For Each objIE In CreateObject("Shell.Application").Windows
' loop through all explorer windows to find tokenized instance
If objIE.GetProperty("marker") = strSignature
这里要求的是上面参考链接的完整代码/粘贴,以使问题更加独立。
'UPD2: You wrote that IE gets disconnected as the login pop-up comes in, hypothetically there is a way to catch disconnection, and then get IE instance again. Note this is "abnormal programming" :) I hope this helps:
Option Explicit
Dim objIE, strSignature, strInitType
Set objIE = CreateObject("InternetExplorer.Application") ' create IE instance
objIE.Visible = True
strSignature = Left(CreateObject("Scriptlet.TypeLib").GUID, 38) ' generate uid
objIE.putproperty "marker", strSignature ' tokenize the instance
strInitType = TypeName(objIE) ' get typename
objIE.Navigate "https://www.yahoo.com/"
MsgBox "Initial type = " & TypeName(objIE) ' for visualisation
On Error Resume Next
Do While TypeName(objIE) = strInitType ' wait until typename changes (ActveX disconnection), may cause error 800A000E if not within OERN
WScript.Sleep 10
Loop
MsgBox "Changed type = " & TypeName(objIE) ' for visualisation
Set objIE = Nothing ' excessive statement, just for clearance
Do
For Each objIE In CreateObject("Shell.Application").Windows ' loop through all explorer windows to find tokenized instance
If objIE.getproperty("marker") = strSignature Then ' our instance found
If TypeName(objIE) = strInitType Then Exit Do ' may be excessive type check
End If
Next
WScript.Sleep 10
Loop
MsgBox "Found type = " & TypeName(objIE) ' for visualisation
On Error GoTo 0
Do While objIE.ReadyState <> 4 ' conventional wait if instance not ready
WScript.Sleep 10
Loop
MsgBox "Title = " & objIE.Document.Title ' for visualisation