0

Is there any "reload" event for WebBrowser?

I mean something event which will print some debug message to the console when the page itself reload.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88

2 Answers2

0

You can listen the DocumentCompleted Event, and print to console. This event is fired every time it finishes downloading a document, so it could fire more than once for 1 URL. Just make sure you check the Url property of DocumentCompleted and compare it to your URL.

To print a message for debug:

System.Diagnostics.Debug.WriteLine("Send to debug output.");

Edit: More info: https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted(v=vs.110).aspx

user1845593
  • 1,736
  • 3
  • 20
  • 37
  • Thank you, but I thing, this event not working for pages which contains some FRAMES. When I load page with this FRAMES, I am getting info about DocumentCompleted 3times. –  Sep 06 '16 at 15:57
  • Well, you have to be more explicit in you question then. You should put some code samples, your attempt to solve the problem and information about platform, like is it WPF or Windows.Forms. Can you give us an example of the problem? – user1845593 Sep 06 '16 at 15:59
  • Sorry. I am using Windows.Form and I want code some Event For DocumentCompleted for page, which contains some frames. I don't know how to do it. –  Sep 06 '16 at 16:06
  • I said in my post that the event could be fired more than 1 time per URL, and I also said what you have to do to bypass that. If you navigate to "http//page1" and this fires N times the event, just check the property Url of "WebBrowserDocumentCompletedEventArgs" and compare it to your initial Url that is "http//page1". – user1845593 Sep 06 '16 at 16:16
  • Sure, it will work. But my problem is... that url are always same. Like this: "www.someweb.com/user=name", only content of document is changed –  Sep 06 '16 at 16:19
  • If the URL is the same, I don't know how you can do it. Sorry – user1845593 Sep 07 '16 at 14:10
-1
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["NextLoad"] == true)
        {
            //save to log
        }else 
        {
            Session["NextLoad"] = true;
        }

    }

You could also do based on postback..

protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            //save to log
        }
    }

Or, if you need to persist beyond session and don't want to use cookies, stick the user's ip into the database and check to see if it exists on next visit. If it does, write the log.

protected void Page_Load(object sender, EventArgs e)
    {
       if(!does_User_IP_Exist_In_DB(user_IP)){
            add_User_IP(user_IP);
       }else{
            write_To_Log();
       }
    }

The use of FRAMES changes things. You will want to use:

// event handler for when a document (or frame) has completed its download
Timer m_pageHasntChangedTimer = null;
private void webBrowser_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e ) {
// dynamic pages will often be loaded in parts e.g. multiple frames
// need to check the page has remained static for a while before safely saying it is 'loaded'
// use a timer to do this

// destroy the old timer if it exists
if ( m_pageHasntChangedTimer != null ) {
    m_pageHasntChangedTimer.Dispose();
}

// create a new timer which calls the 'OnWebpageReallyLoaded' method after 200ms
// if additional frame or content is downloads in the meantime, this timer will be destroyed
// and the process repeated
m_pageHasntChangedTimer = new Timer();
EventHandler checker = delegate( object o1, EventArgs e1 ) {
    // only if the page has been stable for 200ms already
    // check the official browser state flag, (euphemistically called) 'Ready'
    // and call our 'OnWebpageReallyLoaded' method
    if ( WebBrowserReadyState.Complete == webBrowser.ReadyState ) {
        m_pageHasntChangedTimer.Dispose();
        OnWebpageReallyLoaded();
    }
};
m_pageHasntChangedTimer.Tick += checker;
m_pageHasntChangedTimer.Interval = 200;
m_pageHasntChangedTimer.Start();
}

OnWebpageReallyLoaded() {
/* place your harvester code here */
}

Taken from: HTML - How do I know when all frames are loaded?

Community
  • 1
  • 1
Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21