Daniel Hamutel
Daniel Hamutel

Reputation: 653

How can i wait until a website is completed loaded?

My code now

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web.UI;

namespace myweb
{
    public partial class Form1 : Form
    {
        static Page page;

        public Form1()
        {
            InitializeComponent();

            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.Navigate("http://www.tapuz.co.il/forums/forumpage/393/%D7%98%D7%91%D7%A2_%D7%95%D7%9E%D7%96%D7%92_%D7%90%D7%95%D7%95%D7%99%D7%A8/%D7%9E%D7%96%D7%92_%D7%94%D7%90%D7%95%D7%95%D7%99%D7%A8");

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            string test "completed";
        }
    }
}

The problem is that it's getting to the DocumentCompleted event few times before the page it self completed loaded. I used a breakpoint on string test = "completed"; and it stopped there few times before the page completly loaded.

I want that it will get there and will make the line string test = "completed"; only once in the end when the webpage is fully loaded.

Upvotes: 0

Views: 110

Answers (2)

WorksOnMyLocal
WorksOnMyLocal

Reputation: 1689

Every time a frame loads, the event is fired.

DocumentCompletemay get fired multiple times for many reasons (frames, ajax, etc). At the same time, for a particular document, window.onload event will be fired only once. So, perhaps, you can do your processing in window.onload. I just tried on doing that below.Hope it helps.

private void Form1_Load(object sender, EventArgs e){
bool complete = false;
this.webBrowser1.DocumentCompleted += delegate
{
    if (complete)
        return;
    complete = true;
    // DocumentCompleted is fired before window.onload and body.onload
    this.webBrowser1.Document.Window.AttachEventHandler("onload", delegate
    {
        // Defer this to make sure all possible onload event handlers got fired
        System.Threading.SynchronizationContext.Current.Post(delegate 
        {
            // try webBrowser1.Document.GetElementById("id") here
            MessageBox.Show("window.onload was fired, can access DOM!");
        }, null);
    });
};

this.webBrowser1.Navigate("http://www.example.com");}

Upvotes: 4

Rahul Tripathi
Rahul Tripathi

Reputation: 172538

Try this:

public Form1()
{
   InitializeComponent();

   webBrowser1.ScriptErrorsSuppressed = true;
   webBrowser1.Navigate("http://www.tapuz.co.il/forums/forumpage/393/%D7%98%D7%91%D7%A2_%D7%95%D7%9E%D7%96%D7%92_%D7%90%D7%95%D7%95%D7%99%D7%A8/%D7%9E%D7%96%D7%92_%D7%94%D7%90%D7%95%D7%95%D7%99%D7%A8");
   while(webBrowser1.ReadyState != WebBrowserReadyState.Complete)
   {
        Application.DoEvents();
   }
   MessageBox.Show("Site Loaded");
}

Upvotes: 0

Related Questions