浏览器控件不等待页面完全加载C#

时间:2015-02-02 23:28:28

标签: c# winforms webbrowser-control

我在Windows窗体中遇到浏览器控件问题。我已经搜索了等待页面完全加载,但它似乎想要在页面加载之前启动。这是代码:

    private void loginToML()
    {

        string userName = Globals.SCMAddin.getUserName();
        string userPassword = Globals.SCMAddin.getUserPassword();

        try
        {
            HtmlDocument doc = browser_Window.Document;
            doc.GetElementById("ctl00_ctl00_cphNestedUtility_cphLeft1_ctlValidateUser_ctl00_txtUserid").SetAttribute("value", userName);
            doc.GetElementById("ctl00_ctl00_cphNestedUtility_cphLeft1_ctlValidateUser_ctl00_btnValidate").InvokeMember("click");

            waitforIE();

            doc = browser_Window.Document;
            doc.GetElementById("ctl00_ctl00_cphNestedUtility_cphLeftContent_ctlValidateUser_ctl00_txtPassword").SetAttribute("value", userPassword);
            doc.GetElementById("ctl00_ctl00_cphNestedUtility_cphLeftContent_ctlValidateUser_ctl00_btnContinue").InvokeMember("click");

            waitforIE();

            this.loggedIn = true;

        }
        catch (Exception loginError)
        {
            throw new Exception("Error Logging In");
        }

    }//END LOGINTOML

    private void waitforIE()
    {
        while (browser_Window.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();
        }
    }//END WAITFORIE

要登录的网站是https://olui2.fs.ml.com/login/login.aspx

我在Excel VBA中使用它,但我已将大部分代码转移到Excel加载项。

0 个答案:

没有答案