WebBrowser 控件,如何确定js 页面载入完成事件

如何判断WebBrowser控件页面是否加载完?【vb吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:109,501贴子:
如何判断WebBrowser控件页面是否加载完?收藏
例如:WebBrowser1.Navigate ""当页面加载完成后WebBrowser1.Navigate ""当页面加载完成后点击页面上的按钮WebBrowser1.Document.getElementByID("newspic").Click-----------------------------------------------------我真的一点思路都没有,要如何判断这个页面加载完?如果用DocumentComplete事件也不行呐有方法判断当前页面是否为某页面?
If WebBrowser1.ReadyState = 4 Then
'我刚才试了用WebBrowser1_DocumentComplete也行啊!!!'------------------------------------------------------------Dim a As IntegerPrivate Sub Form_Load()WebBrowser1.Navigate ""a = 1End SubPrivate Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)&&&& If (pDisp Is WebBrowser1.Object) Then '当网页下载完毕执行下面的语句&&&&&&&& If a = 1 Then '如果当前是第一个页面&&&&&&&&&&&& Call two&&&&&&&& ElseIf a = 2 Then '如果当前是第二个页面&&&&&&&&&&&& WebBrowser1.Document.getElementByID("newsPic").Click&&&&&&&&&&&& a = 0 '到这里不判断了,呵呵&&&&&&&& End If&&&& End IfEnd SubSub two()WebBrowser1.Navigate ""a = 2End Sub
能不能判断页面加载完成?例如百度的网页,有能判断的方法吗?
把我那个放在DocumentComplete事件里试试
你方法我是知道的..你告诉过我的,也用过了但是,只能判断页面加载完成,我想要的效果是加载判断加载某个页面加载完成.能不能if页面上的元素id来实现?
不知道是不是要这个效果Private Sub Form_Load()
WebBrowser1.Navigate ""End SubPrivate Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Debug.Print "当前已加载完成:" & URL
If Not WebBrowser1.ReadyState = 4 Then
Debug.Print "全部加载完成"
End IfEnd Sub
我以前写了个获取WebBrowser1下鼠标处标签属性的工具,在WebBrowser1里动态插入JS脚本或VBS脚本和变量,通过脚本获取页面属性或其它操作。然后VB读取或判断动态插入的变量。 楼主说的这应该也可以这样处理
对于页面里的flash是否加载完全,这个无效
恩,那么我需要加载完第一个页面的时候加载第二个页面,然后加载第三个页面我怎么去判断他?
通用软件不合适,定做软件太昂贵,自己用vb.net开发太难,何不试试FoxTable?
顶,不错正需要
第一个:以前写过一个检测登录百度成功后跳转的,发给你看看,我给注释下吧思路就是:加载完网页后,对网页元素进行分析,判断Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
'--------当加载完'---------获取网页源码----------Dim doc, objhtml As Object
Dim i As Integer
Dim strhtml As String
If Not WebBrowser1.Busy Then
Set doc = WebBrowser1.Document
Set objhtml = doc.body.createTextRange()
If Not IsNull(objhtml) Then
a5 = objhtml.htmlText
'---------获取网页源码----------'--------------开始对网页元素进行判断----------b5 = "你是什么性格?"b1 = "&H2&&A href="c = "的个人资料"
If InStr(a5, b5) && 0 Then '--------------如果网页中找到这个元素,则执行以下操作---------- Command3.Visible = True
Command2.Visible = TruePicture4.Visible = False Picture3.Visible = True
Command4.Visible = True
'--------------这里面可以写很多了。不如调到第二个页面什么的----------End If
End Sub第二个:比较简单明了Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
'--------当加载完If URL = "" Then '--------判断加载完的URL地址 WebBrowser1.Navigate "'--------进行跳转 End If
这个和post登录百度 关键字判断是否登录成功一样
Private Sub WebBrowser1_DocumentComplete()Msgbox "加载完成!"End Sub
对了,上面Complete 括号里加上 (ByVal pDisp As Object, URL As Variant)
登录百度帐号posts - 38,&
comments - 2,&
trackbacks - 0
最近有个小程序需要采集网页源代码,但有的网页中JS脚本又会生成额外的代码,比如http://www.cnblogs.com/lidabo/p/4169396.html
红框部分便是另外加载的代码。
此处可以看到web前端是有 "操作系统" 几个字的,但查看网页源代码之后却搜不到这几个字
C#有个webbrowser控件可以等网页加载完之后获取浏览器上所有的网页源代码(也包括额外被JS加载进来的代码)
【第一次】试验
WebBrowser webBrowser1 = new WebBrowser();
private void button1_Click(object sender, EventArgs e)
  webBrowser1.Navigate("http://www.cnblogs.com/lidabo/p/4169396.html");
  //加载完毕后触发事件webBrowser1_DocumentCompleted
  webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
private void webBrowser1_DocumentCompleted(object sender, EventArgs e)//这个就是当网页载入完毕后要进行的操作
  //将webBrowser显示的代码传入richTextBox以便调试
  richTextBox1.Text = webBrowser1.DocumentT
结果:webbrowser加载完这个页面之后,richTextBox1.Text获取的代码里面找不到  "操作系统"  几个字 , 获取的代码有1063行
网页在加载的过程中,webBrowser1_DocumentCompleted可能会触发一次以上,且相同网页可能触发次数不尽相同
有的网页会多次触发webBrowser1_DocumentCompleted,腾讯网http://www.qq.com触发了4次
【第二次】试验
//设置全局变量i
private void webBrowser1_DocumentCompleted(object sender, EventArgs e){
  i++;//以便观察这个事件被触发了多少次
  richTextBox1.Text = webBrowser1.DocumentT
结果:最后 &i &的值为 1,richTextBox1.Text获取的代码里面依然找不到  "操作系统"  几个字 , 获取的代码有1063行
按理来说不应该只触发一次、
【第三次】试验
private void webBrowser1_DocumentCompleted(object sender, EventArgs e){
  MessageBox.Show("111");//这个得迅速点掉确定才看到触发了多少次messagebox
  richTextBox1.Text = webBrowser1.DocumentT
&结果:迅速点掉messagebox的“确定”后又出现了一次messagebox,即一共两次触发了webBrowser1_DocumentCompleted事件
实验触发二次webBrowser1_DocumentCompleted事件,里面可以找到"操作系统"几个字,代码共有2095行,这次应该全部获取完毕了
加入没有迅速点掉messagebox,可能这个等待过程中,网页可能被webbrowser真的完全加载完毕从而不再触发webBrowser1_DocumentCompleted事件
【第二次】实验只触发一次webBrowser1_DocumentCompleted事件,而且获取的代码还不完全
【第三次】实验加入了messagebox(然后迅速点掉)能触发一次以上的webBrowser1_DocumentCompleted事件,获取代码完全
【第三次】实验加入了messagebox(没有迅速点掉)只能触发一次webBrowser1_DocumentCompleted事件,获取代码完全
我没弄明白
***********************************************************************************************************
那么换个思路。在第一次进入webBrowser1_DocumentCompleted事件之时开启一个timer时钟,间隔5秒,5秒之后获取一次webbrowser的代码
只触发一次时钟时间,由于时钟是另外的线程,要调用webbrowser需要用到委托
//实例化timer
System.Timers.Timer t = new System.Timers.Timer();
//定义委托变量
public happy updateTxt_
public delegate void happy();
public Form1(){  InitializeComponent();
  //设定timer  t.Interval = 5000; //定时,单位ms  t.Enabled = //回调函数允许执行  t.AutoReset = //回调函数循环执行  t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到达时间的时候执行事件;&  t.Stop();}
private void Form1_Load(object sender, EventArgs e)
  updateTxt_ok = new happy(UpdateTxtMethod);
//委托执行函数
public void UpdateTxtMethod()
  //此处不用DocumentText是因为它获取中文可能产生乱码
  Encoding encoding = Encoding.GetEncoding(webBrowser1.Document.Encoding);
  StreamReader stream = new StreamReader(webBrowser1.DocumentStream, encoding);
  string aa = stream.ReadToEnd();
  richTextBox1.Text =
  stream.Close();
//timer事件
public void theout(object source, System.Timers.ElapsedEventArgs e)
  this.BeginInvoke(updateTxt_ok);//委托执行updateTxt_ok
//按下按钮
private void button1_Click(object sender, EventArgs e)
  webBrowser1.Navigate(richTextBox1.Text);
  webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
private void webBrowser1_DocumentCompleted(object sender, EventArgs e)//这个就是当网页载入完毕后要进行的操作
{  t.start();//开启时钟
结果:获取完整源代码,共有2095行
但不能保证其他网页也是延迟5秒就能加载完,有的服务器很卡,需要调整时间
阅读(...) 评论()怎么判断WebBrowser中的网页是否加载完毕_百度知道
怎么判断WebBrowser中的网页是否加载完毕
我有更好的答案
怎么判断WebBrowser中的网页是否加载完毕private void Delay(int Millisecond) //延迟系统时间,但系统又能同时能执行其它任务;
DateTime current = DateTime.N
while (current.AddMilliseconds(Millisecond) & DateTime.Now)
Application.DoEvents();//转让控制权
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。Webbrowser控件判断网页加载完毕的简单方法
转自:http://blog.csdn.net/cometnet/article/details/5261192
一般情况下,当ReadyState属性变成READYSTATE_COMPLETE时,Webbrowser控件会通过触发DocumentCompleted事件来指示网页加载完毕。但当加载的网页包含frame时,可能会多次触发该事件,所以不能简单地通过它来判断网页加载完毕。
从微软的官方网站上了解到,并非每个frame都对应了一个DocumentCompleted事件,只有触发了DownloadBegin事件的frame才会有相应的DocumentCompleted事件。另外,最外层的frame总是最后触发DocumentCompleted事件。DocumentCompleted事件具有一个IDispatch *类型的参数,它指示了是在哪个frame上触发的该事件。所以,要判断文档是否加载完毕,只需要判断IDispatch *参数是否是Webbrowser控件的IDispatch。
微软support网站上关于这个问题的说明:
原文及译文如下:(我用Google翻译的,不一定正确,大家见谅)
The Internet Explorer WebBrowser control fires the DocumentComplete event when it is finished downloading a Web page. You can create a event handler function in your application for this event. This article describes the steps to take in determining if a the
WebBrowser control is finished downloading a Web page.
在Internet Explorer WebBrowser控件激发DocumentComplete事件完成时下载网页。您可以在应用程序中创建的这一事件的事件处理函数。本文介绍的步骤可以参与决定,如果WebBrowser控件下载完成一个网页。
The WebBrowser control fires the DocumentComplete event when its ReadyState property is changed to READYSTATE_COMPLETE. This indicates that the WebBrowser control has completed downloading the Web page. Here are some important points regarding this event:
In the case of a page with no frames, DocumentComplete is fired once after everything is done.In case of multiple frames, DocumentComplete gets fired multiple times. Not every frame fires this event, but each frame that fires a DownloadBegin event fires a corresponding DocumentComplete event.The DocumentComplete event has a IDispatch* parameter, which is the IDispatch of the frame (shdocvw) for which DocumentComplete is fired.The top-level frame fires the DocumentComplete in the end. So, to check if a page is done downloading, you need to check if the IDispatch* parameter is same as the IDispatch of the WebBrowser control.
WebBrowser控件激发DocumentComplete事件时,它的readyState属性更改为READYSTATE_COMPLETE。这表明,WebBrowser控件已完成下载的网页。以下是一些对这起事件的要点:在网页的情况下,没有框架,一旦被激发的DocumentComplete一切完成之后。在多个帧的情况,得到的DocumentComplete多次发射。不是每帧触发这一事件,但每个帧触发一个DownloadBegin事件触发相应的DocumentComplete事件。DocumentComplete事件的IDispatch *有一个参数,它是框架(shdocvw),这些被激发的DocumentComplete的IDispatch。在顶级框架火灾最终的DocumentComplete。因此,要检查一个页面完成下载,您需要检查的IDispatch
*参数作为WebBrowser控件的IDispatch相同。
For Visual Basic, here is code that performs this check:
To handle the DocumentComplete event in Visual C++ and determine if the download of the Web page is complete, follow these steps.
Note that the steps you follow depend on the way you use the WebBrowser control.
If you are creating the WebBrowser control in a CWnd/CView object, you must follow steps 1 to 4.If you are creating the WebBrowser control in a CDialog/CFormView object, only need to follow step 4.If you are using the CHtmlView class provided with Visual C++ 6.0, override CHtmlView::DocumentComplete() and follow step 4, using the m_pBrowserApp member of the CHtmlView class to access the WebBrowser control.
为了处理在Visual C + + DocumentComplete事件,并确定如果网页下载完成后,按照下列步骤。请注意,您按照步骤取决于你如何使用WebBrowser控件。如果你在创建一个CWnd / CView对象WebBrowser控件,则必须按照步骤1至4。如果您要创建一个CDialog在WebBrowser控件/ CFormView对象,只需要按照步骤4。如果您使用的是CHtmlView类提供与Visual
C + + 6.0,重写CHtmlView::的DocumentComplete()和后续步骤4,使用该CHtmlView类m_pBrowserApp成员访问WebBrowser控件。
1.Define the OnDocumentComplete method in the header file for your CWnd/CView-derived class:
2.Declare the event sink in the same header file:
3.In the implementation file (.cpp) for your CWnd/CView-derived class, implement the event sink map:
4.Implement the OnDocumentComplete method:
This approach works when the WebBrowser control navigates to a page that changes the top-level frame. Say if the navigation occurs within a frame itself, then the final DocumentComplete that is fired is that of the frame and not the
top-level frame. For example, consider the following scenario.
The WebBrowser control is hosting a frameset. Within one frame of the frameset, the user clicks on a link that opens a new page in the frame itself and keeps the rest of the frameset intact. The new page could contain multiple frames again. So, there will be
multiple DocumentComplete notifications (one for each new frame). But, since the top-level frame has not changed, the final DocumentComplete would be that of the frame that has changed.
If you are interested in checking for the final document complete in this scenario, you could do the following:
Check if the IDispatch parameter of the DocumentComplete is the same as the IDispatch parameter of first NavigateComplete2 event. Since the first NavigateComplete2 is of the top-level frame and the last DocumentComplete is also of the top-level
frame, doing a comparison in such a fashion will tell whether the page is done downloading.
这种方法在WebBrowser控件时,导航到一个网页,更改顶级框架。说,如果出现导航本身的框架内,那么最后一个DocumentComplete是发射的是,框架,而不是顶级框架。例如,考虑以下情况。WebBrowser控件主持一个框架。在一个框架集框架,在用户点击链接打开在框架本身就是一种新的一页,保持完整的框架休息。新的页面可能包含多个帧了。因此,将有多个(每个新的框架之一)的DocumentComplete通知。但是,由于顶级框架没有改变,最后的DocumentComplete将是该改变了框架。如果您是在为最后文件在这种情况下完成检查感兴趣,你可以做以下操作:检查的一个DocumentComplete的IDispatch参数是相同的第一NavigateComplete2事件的IDispatch参数。自第一NavigateComplete2是顶级帧和最后一个DocumentComplete也是顶级框架,做了这样的方式比较,判断该页面进行下载。
Here is some sample C++ code:
没有更多推荐了,

我要回帖

更多关于 页面未检测到控件运行 的文章

 

随机推荐