0

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();
            }
  • You should probably use the WebBrowser's [DocumentCompleted](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.webbrowser.documentcompleted?view=netframework-4.7.2) event. – LarsTech Dec 23 '18 at 19:05
  • @LarsTech I tried to use it. Does not work. Could you tell me how to use it correctly? `private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)` – user9776818 Dec 23 '18 at 19:13
  • Post the code you wrote for the `DocumentCompleted` event. – Jimi Dec 23 '18 at 19:18
  • @Jimi I don't understand how to organize the code. What method to place there? `Test2 ();`? – user9776818 Dec 23 '18 at 19:28
  • You probably want [something like this](https://stackoverflow.com/questions/53213782/how-to-get-real-video-url-video-tag?answertab=active#tab-top). – Jimi Dec 23 '18 at 19:30
  • @Jimi I try to do this, but the code does not work exactly as I need `private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (webBrowser1.ReadyState != WebBrowserReadyState.Complete) return; Authorization(); }` – user9776818 Dec 23 '18 at 19:36
  • Wouldn't there be two document completes? First time loads the login page, and a second time for when a user successfully logs in? You would have to know which page you are looking at. – LarsTech Dec 23 '18 at 19:41
  • @LarsTech I ask you to excuse me, but still I do not understand how to arrange this event correctly. All my attempts do not lead to the result. Could you help or give a hint how to do this? – user9776818 Dec 23 '18 at 20:11
  • We don't see all your attempts. The DocumentCompleted event is pretty simple. When the document is finished loading, the event will run. It's not clear what is null in your code when that event runs. – LarsTech Dec 23 '18 at 20:14
  • @LarsTech Updated question – user9776818 Dec 23 '18 at 20:52

1 Answers1

0

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
    }
I_Al-thamary
  • 3,385
  • 2
  • 24
  • 37