WebBrowser.DocumentCompleted事件未触发

时间:2016-01-11 14:16:00

标签: c# .net winforms internet-explorer webbrowser-control

我有一个Windows窗体应用程序。应用程序运行良好,但有时WebBrowser.DocumentCompleted事件不会触发。我提供了以下代码。在调用方法ClickButton(GetValue("projectadditionalinfo_runID"));后的代码中,WebBrowser.DocumentCompleted事件不会触发。同样奇怪且不一致WebBrowser.DocumentCompleted即使在调用方法ClickButton(GetValue("projectadditionalinfo_runID"));之前,事件也不会触发。整个程序停留在while循环中,因为WebBrowser.DocumentCompleted事件没有触发。我错过了什么?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.Configuration;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private static bool completed = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void WaitForNavigation()
        {
            Debug.WriteLine("Waiting for navigation - started");
            while (!completed)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
            Debug.WriteLine("Waiting for navigation - completed");
        }

        private void scheduleProjectAdditionalInformation()
        {
            Debug.WriteLine("Scheduling project additional information - started");
            completed = false;
            psarm.Navigate(GetValue("projectadditionalinfoLink"));
            WaitForNavigation();
            completed = false;
            ClickButton(GetValue("projectadditionalinfo_runID"));
            WaitForNavigation();
            Debug.WriteLine("Scheduling project additional information - completed");
        }

        private void navigatetopsarm()
        {
            Debug.WriteLine("Navigating to PSA RM - started");
            completed = false;
            psarm.Navigate(GetValue("psarmhomepageLink"));
            WaitForNavigation();
            Debug.WriteLine("Navigating to PSA RM - completed");
        }

        private void logout()
        {
            Debug.WriteLine("Logging out of the ENT portal - started");
            completed = false;
            psarm.Navigate(GetValue("ent_portal_logoutLink"));
            WaitForNavigation();
            Debug.WriteLine("Logging out of the ENT portal - completed");
        }

        private void login()
        {
            Debug.WriteLine("Logging in ENT portal - started");
            completed = false;
            psarm.Navigate(GetValue("ent_portal_homepageLink"));
            WaitForNavigation();
            fill(GetValue("ent_portal_homepage_usernameID"), GetValue("ent_portal_homepageUsername"));
            fill(GetValue("ent_portal_homepage_passwordID"), GetValue("ent_portal_homepageUsepassword"));
            ClickButton(GetValue("ent_portal_homepage_submit_ButtonValue"));
            WaitForNavigation();
            Debug.WriteLine("Logging in ENT portal - completed");
        }

        private void ClickButton(string buttonValue)
        {
            completed = false;
            HtmlElementCollection elc = psarm.Document.GetElementsByTagName("input");
            foreach (HtmlElement el in elc)
            {
                if (el.GetAttribute("type").Equals("submit") && el.GetAttribute("value").Equals(buttonValue))
                {
                    el.InvokeMember("Click");
                }
            }
        }

        private void ClickLink(string value, bool removetarget)
        {
            completed = false;
            HtmlElementCollection elc = psarm.Document.GetElementsByTagName("a");
            foreach (HtmlElement el in elc)
            {
                Debug.WriteLine(el.OuterHtml);
                if ((el.GetAttribute("title") != null && (el.GetAttribute("title").Equals(value))) || (el.InnerText != null && el.InnerText.Equals(value)))
                {
                    if (removetarget)
                        el.SetAttribute("target", "");
                    el.InvokeMember("Click");
                    break;
                }
            }
        }

        private void submitform(string formID)
        {
            completed = false;
            HtmlElement form = psarm.Document.GetElementById(formID);
            if (form != null)
                form.InvokeMember("submit");
        }

        private void fill(string key, string value)
        {
            HtmlElement element = psarm.Document.GetElementById(key);
            if (element != null)
                element.SetAttribute("value", value);
        }

        void psarm_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if ((sender as WebBrowser).ReadyState == System.Windows.Forms.WebBrowserReadyState.Complete)
            {
                completed = true;
            }
        }

        private string GetValue(string KeyName)
        {
            return ConfigurationManager.AppSettings[KeyName];
        }

        private void StartProcessing_Click(object sender, EventArgs e)
        {
#if DEBUG
            psarm.Visible = true;
            psarm.ScriptErrorsSuppressed = true;
            psarm.ScrollBarsEnabled = false;
#endif
            psarm.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(psarm_DocumentCompleted);
            psarm.Navigating += new WebBrowserNavigatingEventHandler(psarm_Navigating);
            psarm.Navigated += new WebBrowserNavigatedEventHandler(psarm_Navigated);
            login();
            WaitForNavigation();
            navigatetopsarm();
            scheduleProjectAdditionalInformation();
        }
    }
}

0 个答案:

没有答案