Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make WebBrowser wait till it loads fully?

I have a C# form with a web browser control on it.

I am trying to visit different websites in a loop.

However, I can not control URL address to load into my form web browser element.

This is the function I am using for navigating through URL addresses:

public String WebNavigateBrowser(String urlString, WebBrowser wb)
{
    string data = "";
    wb.Navigate(urlString);
    while (wb.ReadyState != WebBrowserReadyState.Complete)
    {
        Application.DoEvents();
    }
    data = wb.DocumentText;
    return data;
}

How can I make my loop wait until it fully loads?

My loop is something like this:

foreach (string urlAddresses in urls)
{
    WebNavigateBrowser(urlAddresses, webBrowser1);
    // I need to add a code to make webbrowser in Form to wait till it loads
}
like image 999
Val Nolav Avatar asked Mar 19 '12 19:03

Val Nolav


People also ask

What happens if the page doesn't finish loading?

If the page never finishes loading, they never see the page. If the page takes too long to load, they may assume something went wrong and just go elsewhere instead of *please wait...*ing. One way of getting around the no javascript issue with this solution would be to make use of the <noscript> tags to "undo" the display:none effect.

How do I know if a link is fully loaded?

On the other hand, if the user loads the link in a background tab, it will display as loading to the browser, showing the animated symbol in the tab, until it is truly fully loaded. Once it displays as loaded in the tab, if they click it, it will be fully loaded.

How to prevent JavaScript events when a page is fully loaded?

One is to prevent people from clicking on links/causing JavaScript events to occur until all the page elements and JavaScript have loaded. In IE, you could use page transitions which mean the page doesn't display until it's fully loaded: Notice the short duration. It's just enough to make sure the page doesn't display until it's fully loaded.

Why doesn't the user ever see the page after loading?

If the user has JavaScript disabled, they never see the page. If the page never finishes loading, they never see the page. If the page takes too long to load, they may assume something went wrong and just go elsewhere instead of *please wait...*ing.


1 Answers

Add This to your code:

webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

Fill in this function

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
     //This line is so you only do the event once   
     if (e.Url != webBrowser1.Url) 
        return;


        //do you actual code        



    }
like image 178
SamFisher83 Avatar answered Nov 15 '22 04:11

SamFisher83