【问题标题】:Resume Macro after IE Crash or not respondIE 崩溃后恢复宏或无响应
【发布时间】:2018-06-18 19:58:01
【问题描述】:

我需要从网站获取数据 我正在使用SHDocVw.InternetExplorerMSHTML.HTMLDocument 并获取数据。 在获取每条记录并为每条记录创建新实例后,我正在使用 set IE = NothingIE.Quit,但只要 IE 停止响应或崩溃,宏就会停止。 我只想在 IE 崩溃时跳过该记录并从下一个记录恢复 是否可以忽略来自 IE 的每个弹出窗口并从下一条记录恢复以获取数据。

For Each Cell In Selection

    URL = Cell.Value
Set IE = New SHDocVw.InternetExplorer
        With IE
        .navigate URL
        .Visible = True
        Do While .readyState <> READYSTATE_COMPLETE Or .Busy = True
            DoEvents
        Loop

        Application.Wait (Now + TimeValue("00:00:05"))

        Set Doc = .document
'*Fetch the Elements and Records by using getElementbyid*
End With
Set IE = Nothing
IE.quit
Next Cell

我也尝试过使用 selenium,但面临同样的问题。 我只想忽略 IE 是否崩溃或不响应特定记录并获取下一个记录。 IE 在 2000 条记录和 100 条记录之后崩溃。 我需要在一夜之间运行这个宏。 我正在使用 IE 11 和 Excel 2010

【问题讨论】:

    标签: vba selenium internet-explorer web-scraping internet-explorer-11


    【解决方案1】:

    您当然可以对此进行改进,但这里有一个基本的开始方法。

    请注意,我们不会一直创建和销毁 IE 实例。我们创建一个并使用它来处理各种 URL,然后在最后关闭它。

    一个简单的On Error GoTo Label 用于处理故障。然后清除错误。标签将程序带到下一个循环。

    可能会引入一个超时循环来尝试设置元素,即Do : On Error Resume Next : Set a = .document.getElemmentById("id") : On Error GoTo O : Loop While a Is Nothing,但在循环内有一个超时子句以避免永远循环。

    对于IE来说是挂机部分是很难说的。也许分块处理 URL,并结合上面提到的超时循环,尝试将元素设置为变量?

    总体而言,如果可能,请确定崩溃的原因并针对您的解决方案进行处理。

    Option Explicit
    
    Public Sub test()
        Dim IE As Object, doc As Object
        Set IE = New SHDocVw.InternetExplorer
    
        With IE
            .Visible = True
            Dim currentCell As Range, URL As String
            Application.DisplayAlerts = False
            On Error GoTo nextURL
            For Each currentCell In Selection
                URL = currentCell.Value
                .navigate URL
    
                While .Busy Or .readyState < 4: DoEvents: Wend
    
                Application.Wait (Now + TimeValue("00:00:05"))
                Set doc = .document
    
                'Attempt to getElementByID
    nextURL:
                If Err.Number <> 0 Then Err.Clear
                If IE Is Nothing Then Set IE = New SHDocVw.InternetExplorer
            Next currentCell
        End With
       IE.Quit
       Application.DisplayAlerts = True
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多