等待 Internet Explorer 加载所有内容? [英] Wait for internet explorer to load everything?

查看:36
本文介绍了等待 Internet Explorer 加载所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓取网页,并等待 Internet Explorer 完成加载,但由于某种原因,它没有完成.我试图在页面上获取一个值,但等待部分没有等待,因此当应该有一个值时,该值返回空白.IE 页面已完成加载,但页面上元素的值尚未加载. 有没有办法在继续下一行代码之前等待所有元素完成加载?这是我的代码:

将 IE 变暗为对象将 myvalue 调暗为字符串IE = CreateObject("internetexplorer.application")IE.navigate("我的页面")虽然不是 IE.ReadyState = WebBrowserReadyState.CompleteApplication.DoEvents()结束时间myValue = IE.document.getElementById("theValue").getAttribute("value")Debug.Print(myValue)

解决方案

不应该使用 Application.DoEvents() 来保持你的 UI 响应!我真的不能强调这一点!经常使用它是一个糟糕的黑客,它只会产生比它解决的更多的问题.

更多信息请参考:

  • SHDocVw 命名空间导入到您要使用它的文件中,并创建一个 InternetExplorer<类型的类级 WithEvents 变量/code> 以便您可以使用 句柄子句.

  • 然后瞧!

    导入 SHDocVw公开课表1Dim WithEvents IE 作为新的 Internet ExplorerPrivate Sub Button1_Click(sender As System.Object, e As System.EventArgs) 处理Button1.ClickIE.Navigate("http://www.google.com/")结束子Private Sub IE_DocumentComplete(pDisp As Object, ByRef URL As Object) 处理 IE.DocumentCompleteMessageBox.Show("成功导航到:" & URL.ToString())结束子结束类

    或者,您也可以使用 lambda 表达式在线订阅事件:

    导入 SHDocVw公开课表1Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) 处理Button1.ClickDim IE 作为新的 Internet ExplorerIE.Navigate("http://www.google.com/")AddHandler IE.DocumentComplete, Sub(pDisp As Object, ByRef URL As Object)MessageBox.Show("成功导航到:" & URL.ToString())结束子结束子结束类

    I am scraping a webpage, and waiting for internet explorer to get done loading but for some reason it is not. I'm trying to get a value on the page but the wait part is not waiting therefore the value comes back blank when there should be a value. The IE page has done loading but the value for the elements on the page has not been loaded yet. Is there a way to wait for all elements to get done loading before proceeding to next line of code? Here's my code:

    Dim IE As Object 
    Dim myvalue as string
    
    IE = CreateObject("internetexplorer.application")
    IE.navigate("mypage")
    
    While Not IE.ReadyState = WebBrowserReadyState.Complete
        Application.DoEvents()
    End While
    
    myValue = IE.document.getElementById("theValue").getAttribute("value")
    Debug.Print(myValue)
    

    解决方案

    You SHOULD NOT use Application.DoEvents() in order to keep your UI responsive! I really can't stress this enough! More than often using it is a bad hack which only creates more problems than it solves.

    For more information please refer to: Keeping your UI Responsive and the Dangers of Application.DoEvents.

    The correct way is to use the InternetExplorer.DocumentComplete event, which is raised when the page (or a sub-part of it, such an an iframe) is completely loaded. Here's a brief example of how you can use it:

    1. Right-click your project in the Solution Explorer and press Add Reference...

    2. Go to the COM tab, find the reference called Microsoft Internet Controls and press OK.

    3. Import the SHDocVw namespace to the file where you are going to use this, and create a class-level WithEvents variable of type InternetExplorer so that you can subscribe to the event with the Handles clause.

    And voila!

    Imports SHDocVw
    
    Public Class Form1
    
        Dim WithEvents IE As New InternetExplorer
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            IE.Navigate("http://www.google.com/")
        End Sub
    
        Private Sub IE_DocumentComplete(pDisp As Object, ByRef URL As Object) Handles IE.DocumentComplete
            MessageBox.Show("Successfully navigated to: " & URL.ToString())
        End Sub
    End Class
    

    Alternatively you can also subscribe to the event in-line using a lambda expression:

    Imports SHDocVw
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim IE As New InternetExplorer
            IE.Navigate("http://www.google.com/")
    
            AddHandler IE.DocumentComplete, Sub(pDisp As Object, ByRef URL As Object)
                                                MessageBox.Show("Successfully navigated to: " & URL.ToString())
                                            End Sub
        End Sub
    End Class
    

    这篇关于等待 Internet Explorer 加载所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆