等待 Internet Explorer 加载所有内容? [英] Wait for internet explorer to load everything?
问题描述
我正在抓取网页,并等待 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:
Right-click your project in the
Solution Explorer
and pressAdd Reference...
Go to the
COM
tab, find the reference calledMicrosoft Internet Controls
and pressOK
.Import the
SHDocVw
namespace to the file where you are going to use this, and create a class-levelWithEvents
variable of typeInternetExplorer
so that you can subscribe to the event with theHandles
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屋!