vasa911
vasa911

Reputation: 79

Wait for loading page after button click

I want to be able to click on button and than wait for the page to load. I search, but there is no solution that work correctly. This is part of code:

el = webBrowser1.Document.GetElementById("LoginButton");
el.InvokeMember("click");
webBrowser1.Navigate(url);

I need it to be that after click, the application will load the page, and only then go to another page. Also, webBrowser1.Navigate(url) must be in the same method as click. Because it all in a loop.

Please help.

EDITED. *More code* (first code was and example of what i need)

List<string> list1 = new List<string>();

            bool flag = true;

            while (flag)
            {
                flag = false;

                foreach (HtmlElement he2 in webBrowser1.Document.GetElementsByTagName("a"))
                {
                    if (he2.GetAttribute("href").Contains("profile.php?ID="))
                    {
                        list1.Add(he2.InnerText);
                    }

                }

                foreach (HtmlElement he in webBrowser1.Document.GetElementsByTagName("a"))
                {

                    if (he.InnerHtml == "Next")
                    {
                        flag = true;

                        he.InvokeMember("click");


                    }
                }


            }

When I click, next page must load, and i have to parse tag on next page, but it begin parse page before next page is load, so it parse the same page.

Upvotes: 0

Views: 3528

Answers (2)

vasa911
vasa911

Reputation: 79

Finally, I just got href from my button and used this code(insted of he.InvokeMember("click")) :

 webBrowser1.Navigate(url_next);
 while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
     Application.DoEvents();

It works fine.

Upvotes: 1

Rikki
Rikki

Reputation: 646

You can use the DocumentCompleted event to find out when the login has completed.

webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
el = webBrowser1.Document.GetElementById("LoginButton");
el.InvokeMember("click");

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    Uri myUrl = new Uri("http://stackoverflow.com");
    if (e.Url == myUrl)
    {
        WebBrowser1.Navigate(url);
    }
}

Upvotes: 0

Related Questions