简体   繁体   中英

Element “webBrowser”. How to wait for the page to load?

Document structure
"Form0" - Form
- - "panel2" - Panel
- - - "Frm5UC" - Custom item
- - - - "webBrowser1" - Browser

Application logic:
- go to the page in "webBrowser1";
- enter login;
- enter the password;
- click the "Login" button.

If I execute the logic through the code (this is the "Method_0 ()" method), the form does not have time to load in the "Authorization ()" method. I get "webBrowser1.Document = null", error "Object link does not indicate an object instance."

If I do everything through the interface, then everything works.

How to make the logic run programmatically?

private void Frm5UC_Load(object sender, EventArgs e)
        {
            webBrowser1.Visible = true;

            // *** TESTS ***
            Method_0();
        }



        #region *** TESTS ***
        public void Method_0()
        {
            Method_1();
            // Method_2();
        }

        public void  Method_1()
        {
            textBox2.Text = "_domain_com";
            textBox1.Text = @"domain_com/login/";

            button1.PerformClick();
        }

        public void Method_2() // Авторизация
        {
            Authorization();
        }
        #endregion *** TESTS ***


        private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(textBox1.Text);            
        }       

        private void button2_Click(object sender, EventArgs e)
        {
            Authorization();
        }       

        public void Authorization() // Авторизация
        {            
                foreach (HtmlElement he in webBrowser1.Document.GetElementsByTagName("input"))
                {
                    if (he.GetAttribute("name") == "login[login]")
                    {
                        he.SetAttribute("value", "login798");
                    }
                }

            // Code "enter password"
            // Code "Press the button"
        }

Update.
I try to use the event "DocumentCompleted".
Added the variable "bool statusAuthorization;".

As a result: - a page with fields for entering login / password opens; and nothing else happens. The code does not enter the login / password.

I try to do debugging.
Steps through the entire code.
No errors. Everything works, but the form with the browser does not open.

If I log in through the interface, then everything works.

bool statusAuthorization;

    private void Frm5UC_Load(object sender, EventArgs e)
    {
            webBrowser1.Visible = true;

            statusAuthorization = true; // !!! CHANGES

            // *** ТЕСТ ***
            Method_0();
    }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (webBrowser1.ReadyState != WebBrowserReadyState.Complete) return;

        if (statusAuthorization == true)
        {
            Authorization();
        }
    }

        #region *** TESTS ***
        public void Method_0()
        {
            Method_1();
            // Method_2();
        }

        public void  Method_1()
        {
            textBox2.Text = "_domain_com";
            textBox1.Text = @"domain_com/login/";

            button1.PerformClick();
        }

        public void Method_2() // Авторизация
        {
            Authorization();
        }
        #endregion *** TESTS ***


        private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(textBox1.Text);            
        }       

        private void button2_Click(object sender, EventArgs e)
        {
            Authorization();
        }       

        public void Authorization() // Авторизация
        {            
                foreach (HtmlElement he in webBrowser1.Document.GetElementsByTagName("input"))
                {
                    if (he.GetAttribute("name") == "login[login]")
                    {
                        he.SetAttribute("value", "login798");
                    }
                }

            // Code "enter password"
            // Code "Press the button"

            statusAuthorization = false; // !!! CHANGES
        }

Update
Result: the form loads again and again.

private void Frm5UC_Load(object sender, EventArgs e)
        {
            string s = "stop";
            webBrowser1.Visible = true;    

            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompletedHandler);

            // *** ТЕСТ ***
              Method_1();
        }

     private void DocumentCompletedHandler(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                //Done!
                Authorization();
            }

Try this:

bool statusAuthorization;

private void Frm5UC_Load(object sender, EventArgs e)
{
        webBrowser1.Visible = true;

        statusAuthorization = true; // !!! CHANGES

        // *** ТЕСТ ***

}

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete) return;

    if (statusAuthorization == true && webBrowser1.Document != null)
    { Method_1();
        Authorization();
    }
}

    #region *** TESTS ***


    public void  Method_1()
    {
        textBox2.Text = "_domain_com";
        textBox1.Text = @"domain_com/login/";


    }






    private void button1_Click(object sender, EventArgs e)
    {Method_1();
        Authorization();
    }       

    public void Authorization() // Авторизация
    {            
            foreach (HtmlElement he in webBrowser1.Document.GetElementsByTagName("input"))
            {
                if (he.GetAttribute("name") == "login[login]")
                {
                    he.SetAttribute("value", "login798");
                }
            }

        // Code "enter password"
        // Code "Press the button"

        statusAuthorization = false; // !!! CHANGES
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM