【问题标题】:Element "webBrowser". How to wait for the page to load?元素“网络浏览器”。如何等待页面加载?
【发布时间】:2018-12-23 18:54:39
【问题描述】:

文档结构
"Form0" - 表格
- - "panel2" - 面板
- - - "Frm5UC" - 自定义项目
- - - - "webBrowser1" - 浏览器

应用逻辑:
- 转到“webBrowser1”中的页面;
- 输入登录名;
- 输入密码;
- 点击“登录”按钮。

如果我通过代码执行逻辑(这是“Method_0()”方法),表单没有时间在“Authorization()”方法中加载。 我得到“webBrowser1.Document = null”, 错误“对象链接不指示对象实例。”

如果我通过界面执行所有操作,那么一切正常。

如何使逻辑以编程方式运行?

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"
        }

更新。
我尝试使用事件“DocumentCompleted”。
添加变量“bool statusAuthorization;”。

结果: - 一个用于输入登录名/密码字段的页面打开; 没有其他事情发生。 验证码不输入登录名/密码。

我尝试进行调试。
逐步执行整个代码。
没有错误。一切正常,但浏览器的表单无法打开。

如果我通过界面登录,那么一切正常。

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
        }

更新
结果:表单一次又一次地加载。

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();
            }

【问题讨论】:

  • 您应该使用 WebBrowser 的 DocumentCompleted 事件。
  • @LarsTech 我尝试使用它。不工作。你能告诉我如何正确使用它吗? private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  • 发布您为DocumentCompleted 事件编写的代码。
  • @Jimi 我不明白如何组织代码。用什么方法放在那里? Test2 ();?
  • 你可能想要something like this。

标签: c# winforms


【解决方案1】:

试试这个:

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
    }

【讨论】:

  • 对不起,我问错了。这正是我所做的。你打算这样做吗? public void Method_0 ()     {         Method_1 ();     } 如果是,那么我做到了。
  • @user9776818 是的,你还有两个函数button1_Click。
  • you have two functions button1_Click - 已更正。抱歉,我再次注意到您的回答重复了我的更新并且不会产生积极的结果。如果您提供其他建议,我将不胜感激。
  • 你是否也删除了button1.PerformClick();
  • 如果它不起作用检查错误并执行if (webBrowser1.Document != null && webBrowser1.Document.Body != null)
猜你喜欢
  • 2013-05-19
  • 1970-01-01
  • 2017-04-17
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
相关资源
最近更新 更多