首页 > 解决方案 > Internet Explorer readyState 从 Complete 恢复为 Interactive

问题描述

我在 IE 中提交表单并等待 IE 返回结果。但是,我的网络上的客户端没有一致的结果。对我来说,这是一致的。

readyState恢复CompleteInteractive。为什么会这样做?

代码:

'submit form
ie_browser.document.getElementsByTagName("form")(0).submit
debug.print "Submitted form"

debug.print "Waiting for IE - 1 - readyState: " & ie_browser.readyState

Do While ie_browser.readyState = 4: DoEvents: Loop
debug.print "Waiting for IE - 2 - readyState: " & ie_browser.readyState

Do Until ie_browser.readyState = 4: DoEvents: Loop
debug.print "Waiting for IE - 3 - readyState: " & ie_browser.readyState

Do Until ie_browser.Busy = False: DoEvents: Loop
debug.print "Waiting for IE - 4 - readyState: " & ie_browser.readyState

输出:

Submitted form
Waiting for IE - 1 - readyState: 4
Waiting for IE - 2 - readyState: 3

编辑#1

为了我:

Submitted form
Waiting for IE - 1 - readyState: 1
Waiting for IE - 2 - readyState: 1
Waiting for IE - 3 - readyState: 4
Waiting for IE - 4 - readyState: 4

编辑#2

弹出窗口看起来像这样。它正在使用该alert()功能。

在此处输入图像描述

编辑#3

我的小测试用例:

Option Explicit

Sub test()

    Dim ie As InternetExplorer
    Dim html As Object
    
    Set ie = CreateObject("InternetExplorer.Application")
    
    'Hide and silence IE
    ie.Visible = False
    ie.Silent = True
    
    ie.navigate "http://127.0.0.1:9000/ftw/bad.html"
    Debug.Print "Navigated..."
    
    Debug.Print "Start wait..."
    Call waitForIE(ie)
    Debug.Print "End wait..."
        
    ie.Quit
    Set ie = Nothing
    
    

End Sub

Public Sub waitForIE(i As InternetExplorer)
    'Craziest workaround ever due to bugged IE Eventhandling
    Do While i.readyState = 4: DoEvents: Loop

    'popup occurred!
    If i.readyState = 3 Then
        Debug.Print "Popup! Stopped IE!"
        i.Stop
    Else
        Do Until i.readyState = 4: DoEvents: Loop
        Do Until i.Busy = False: DoEvents: Loop
        
        Debug.Print "Browser should have parsed and rendered the page at this time"
        
        Debug.Print "IE State: " & i.readyState & " IE busy: " & i.Busy
        
        'IE info
        Debug.Print "IE CompatMode: " & i.document.compatMode
        Debug.Print "IE DocumentMode: " & i.document.documentMode
    End If
    
End Sub

带有弹出窗口的 HTML(基于我正在查询的平台):

<html>
<head>
<title>Bad file</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Bad!
<script type="text/javascript">
    alert("Hello World!");
</script>
</body>
</html>

编辑#4

当我拿起弹出窗口时,我会杀死 IE:

Option Explicit

Sub test()

    Dim ie As InternetExplorer
    Dim html As Object
    
    Set ie = CreateObject("InternetExplorer.Application")
    
    'Hide and silence IE
    ie.Visible = False
    ie.Silent = True
    
    ie.navigate "http://127.0.0.1:9000/ftw/bad.html"
    Debug.Print "Navigated..."
    
    Debug.Print "Start wait..."
    Call waitForIE(ie)
    Debug.Print "End wait..."
        
    'close if found
    If Not ie Is Nothing Then
        ie.Quit
        Set ie = Nothing
    End If
    
    

End Sub

Public Sub waitForIE(i As InternetExplorer)
    'Craziest workaround ever due to bugged IE Eventhandling
    Do While i.readyState = 4: DoEvents: Loop

    'popup occurred!
    If i.readyState = 3 Then
        Debug.Print "Popup! Killed IE!"
        i.Stop
        i.Quit
        Set i = Nothing
    Else
        Do Until i.readyState = 4: DoEvents: Loop
        Do Until i.Busy = False: DoEvents: Loop
        
        Debug.Print "Browser should have parsed and rendered the page at this time"
        
        Debug.Print "IE State: " & i.readyState & " IE busy: " & i.Busy
        
        'IE info
        Debug.Print "IE CompatMode: " & i.document.compatMode
        Debug.Print "IE DocumentMode: " & i.document.documentMode
    End If
    
End Sub

但是,我确实得到了一些偷偷摸摸的弹出窗口:

在此处输入图像描述

编辑#5

显然,杀死 IE 并不能真正杀死它。我在任务管理器中看到多个实例。我必须将它们带到前台才能看到弹出窗口,然后单击按钮关闭弹出窗口以查看进程消失。

编辑#6

我找到了一个循环关闭所有 IE 实例的函数(IE_Sledgehammer),但就像最后一张海报一样,我的函数也挂起:

https://www.mrexcel.com/board/threads/close-all-ie-browser-windows-with-vba.229394/

编辑#7

我一直在试验KillHwndProcess这里找到的功能: https ://www.devhut.net/2018/09/09/vba-forcing-applications-to-close/

If i.readyState = 3 Then
    Debug.Print "Popup!"
    ie_hwnd = i.hwnd
    Debug.Print "ie_hwnd: " & ie_hwnd
    i.Quit
    Set i = Nothing
    Debug.Print "Quit IE, set to nothing..."
    Call KillHwndProcess(ie_hwnd)
    Debug.Print "Killed IE process..."
Then

标签: excelvbainternet-explorer

解决方案


推荐阅读