检测何时加载网页而不使用睡眠

我在Windows上创build一个VB脚本,在IE中打开一个网站。 我想要什么:检测何时加载网页并显示一条消息。 我通过使用睡眠( WScript.Sleep )约达到了这一点。 当网站被加载时的秒数。 但是,该网站在中途popup用户名,密码。 只有当用户input凭证时,才能完成加载页面。 所以我不想使用“睡眠”大约几秒钟,而是一个确切的function或一种方式来检测页面加载。 我在线检查,并尝试使用Do While循环, onloadonclick函数,但没有任何工作。 为了简化,即使我写了一个脚本来打开一个像雅虎这样的网站,并检测到,当页面加载时显示一个消息“嗨”:它不工作,没有使用睡眠( WScript.Sleep )。

尝试常规方法:

 Set objIE = CreateObject("InternetExplorer.Application") objIE.Visible = True objIE.Navigate "https://www.yahoo.com/" Do While objIE.ReadyState <> 4 WScript.Sleep 10 Loop ' your code here ' ... 

UPD:这个应该检查错误:

 Set objIE = CreateObject("InternetExplorer.Application") objIE.Visible = True objIE.Navigate "https://www.yahoo.com/" On Error Resume Next Do If objIE.ReadyState = 4 Then If Err = 0 Then Exit Do Else Err.Clear End If End If WScript.Sleep 10 Loop On Error Goto 0 ' your code here ' ... 

UPD2:你写到IE浏览器会在loginpopup窗口中断开连接,假设有一种方法可以捕捉断开连接,然后再次获取IE实例。 注意这是“exception编程”:)我希望这有助于:

 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 

您可以从DOM获取所有文本节点,链接等,如下所示:

 Option Explicit Dim objIE, colTags, strResult, objTag, objChild, arrResult Set objIE = CreateObject("InternetExplorer.Application") objIE.Visible = True objIE.Navigate "https://www.yahoo.com/" Do While objIE.ReadyState <> 4 WScript.Sleep 10 Loop Set colTags = objIE.Document.GetElementsByTagName("a") strResult = "Total " & colTags.Length & " DOM Anchor Nodes:" & vbCrLf For Each objTag In colTags strResult = strResult & objTag.GetAttribute("href") & vbCrLf Next ShowInNotepad strResult Set colTags = objIE.Document.GetElementsByTagName("*") arrResult = Array() For Each objTag In colTags For Each objChild In objTag.ChildNodes If objChild.NodeType = 3 Then ReDim Preserve arrResult(UBound(arrResult) + 1) arrResult(UBound(arrResult)) = objChild.NodeValue End If Next Next strResult = "Total " & colTags.Length & " DOM object nodes + total " & UBound(arrResult) + 1 & " #text nodes:" & vbCrLf strResult = strResult & Join(arrResult, vbCrLf) ShowInNotepad strResult objIE.Quit Sub ShowInNotepad(strToFile) Dim strTempPath With CreateObject("Scripting.FileSystemObject") strTempPath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%TEMP%") & "\" & .gettempname With .CreateTextFile(strTempPath, True, True) .WriteLine (strToFile) .Close End With CreateObject("WScript.Shell").Run "notepad.exe " & strTempPath, 1, True .DeleteFile (strTempPath) End With End Sub 

也看看获取文本数据

UPD3:我想在这里额外检查网页加载和初始化是否完成:

 ' ... ' Navigating to some url objIE.Navigate strUrl ' Wait for IE ready Do While objIE.ReadyState <> 4 Or objIE.Busy WScript.Sleep 10 Loop ' Wait for document complete Do While objIE.Document.ReadyState <> "complete" WScript.Sleep 10 Loop ' Processing loaded webpage code ' ... 

UPD4:在某些情况下,如果您需要跟踪文档中是否创build了目标节点(通常,在尝试通过.getElementById等访问节点时遇到Object required错误是必需的):

如果页面使用AJAX(加载的页面源代码HTML不包含目标节点,像JavaScript这样的活动内容会dynamic地创build它),那么下面的代码片段就是一个例子,显示了这个代码的样子。 文本节点5.99可能在页面被完全加载之后创build,并且一些其他请求到服务器以显示额外的数据已经占据了一席之地:

 ... <td class="price-label"> <span id="priceblock" class="price-big color"> 5.99 </span> </td> ... 

或者,如果您正在加载Googlesearch结果页面,并等待下一步button出现(特别是,如果您调用上一页的.click方法),或者用loginweb表单加载某个页面并等待用户名input字段,如<input name="userID" id="userID" type="text" maxlength="24" required="" placeholder="Username" autofocus="">

如果目标节点是可访问的,下面的代码允许进行额外的检查:

 With objIE ' Navigating to some url .Navigate strUrl ' Wait for IE ready Do While .ReadyState <> 4 Or .Busy WScript.Sleep 10 Loop ' Wait for document complete Do While .Document.ReadyState <> "complete" WScript.Sleep 10 Loop ' Wait for target node created Do While TypeName(.Document.getElementById("userID")) = "Null" WScript.Sleep 10 Loop ' Processing target node .Document.getElementById("userID").Value = "myusername" ' ... ' End With 

一些networking服务器拒绝服务,并且在请求几千个网页后不加载网页。 我通过closures和打开Internet路由器来获得新的IP地址来解决这个问题。 或清除Cookie。