IE 实例,DocumentCompleted 执行太快

发布于 2024-11-30 05:59:18 字数 1533 浏览 1 评论 0原文

我在程序外部创建了一个 IE 实例,该程序可以正确找到并附加到该实例。我设置了事件处理程序并告诉程序前进到登录屏幕。 DocumentCompleted 句柄应该在网页完全加载时触发,但我的句柄似乎在新页面出现之前触发。该句柄仅触发一次(意味着只有一帧?)。

如果我将其修改为直接从登录页面运行,则该代码可以正常执行。我做错了什么吗?感谢您的帮助:)

        Process.Start(@"IESpecial.exe");

        SHDocVw.ShellWindows allBrowsers = new SHDocVw.ShellWindows();
        while (true)
        {
            foreach (SHDocVw.WebBrowser ie in allBrowsers)
            {
                if (ie.LocationURL == "http://website/home.asp")
                {
                    loggingIn = true;
                    webBrowser = ie;
                    webBrowser.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(webBrowser1_DocumentCompleted);
                    webBrowser.Navigate("http://website/logon.asp");
                    return;
                }
            }
            Thread.Sleep(10);
        }
    }

        private void webBrowser1_DocumentCompleted(object pDisp, ref object URL)
    {
        //we are attempting to log in
        if (loggingIn)
        {
            mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)webBrowser.Document;
            mshtml.HTMLWindow2 window = (mshtml.HTMLWindow2)doc.IHTMLDocument2_parentWindow;

            doc.getElementById("Username").setAttribute("value", "MLAPAGLIA");
            doc.getElementById("Password").setAttribute("value", "PASSWORD");
            window.execScript("SubmitAction()", "javascript");
            loggingIn = false;
            return;
        }

I create an instance of IE outside my program, which the program finds and attaches to correctly. I set up my event handler and tell the program to advance to the login screen. The DocumentCompleted handle is supposed to fire when the web page is completely loaded, but mine seems to be firing before the new page has appeared.. The handle only fires once (meaning there is only one frame?).

This code executes fine if I modify it to work straight from the login page also.. Am I doing something wrong? Thanks for any assistance :)

        Process.Start(@"IESpecial.exe");

        SHDocVw.ShellWindows allBrowsers = new SHDocVw.ShellWindows();
        while (true)
        {
            foreach (SHDocVw.WebBrowser ie in allBrowsers)
            {
                if (ie.LocationURL == "http://website/home.asp")
                {
                    loggingIn = true;
                    webBrowser = ie;
                    webBrowser.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(webBrowser1_DocumentCompleted);
                    webBrowser.Navigate("http://website/logon.asp");
                    return;
                }
            }
            Thread.Sleep(10);
        }
    }

        private void webBrowser1_DocumentCompleted(object pDisp, ref object URL)
    {
        //we are attempting to log in
        if (loggingIn)
        {
            mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)webBrowser.Document;
            mshtml.HTMLWindow2 window = (mshtml.HTMLWindow2)doc.IHTMLDocument2_parentWindow;

            doc.getElementById("Username").setAttribute("value", "MLAPAGLIA");
            doc.getElementById("Password").setAttribute("value", "PASSWORD");
            window.execScript("SubmitAction()", "javascript");
            loggingIn = false;
            return;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文