C#webbrowser documentcomplete事件被触发两次

时间:2014-04-02 14:51:53

标签: c# listbox automation webbrowser-control

我正在创建一个c#窗口表单应用程序,用户可以在其中注册我的网站。该程序注定要简化从桌面进行多次注册的过程。我有两个注册过程的事件。

第一件事:

 private void textBox5_TextChanged(object sender, EventArgs e)
        {


            if (textBox5.Text.Length == 4)
            {
                // users to enter captcha in this field
                button5.Text = "&Copy " + Convert.ToString( listBox2.Items.Count) + " Id";
                registerPost();              

                textBox5.Clear(); //clear the captcha for loading next captcha

            }

        }

第二次活动:

private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            validation();
            captcha();

         }

现在,验证方法包含几个逻辑:

    private void validation()
    {
        if (webBrowser2.DocumentTitle.Contains("Referral via Email"))
        {
            HtmlElementCollection classButton = webBrowser2.Document.All;
            foreach (HtmlElement element in classButton)
            {
                if (element.GetAttribute("className") == "cta-join-btn")
                {
                    element.InvokeMember("click");
                }

            }

        }

        else if (webBrowser2.DocumentText.Contains("Security Code does not match."))
        {
            label9.Text = "&Status : Wrong Captcha";
        }
        else if (webBrowser2.DocumentText.Contains("exceeded"))
        {
            label9.Text = "&Status : Exceeded";


            if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
            {
                listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
            }
            webBrowser2.Navigate(textBox4.Text);
        }
        else if (webBrowser2.DocumentText.Contains("Oops, you took too long to fill up this field. A new code has been generated for you."))
        {
            label9.Text = "&Status : Captcha Expired";
        }
        else if (webBrowser2.DocumentText.Contains("Username is taken."))
        {
            label9.Text = "&Status : UserName Exist";
            textBox3.Text = generateRandomID(Convert.ToInt32(textBox6.Text));
        }
        else if (webBrowser2.DocumentText.Contains("The email address you entered is already linked to an existing mig33 account! Please enter a different email address."))
        {
            label9.Text = "&Status : Email Exist";

            if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
            {
                listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
            }
            webBrowser2.Navigate(textBox4.Text);
        }
        else if (webBrowser2.DocumentTitle.Contains("Success")) //problem area 
        {

            writeText();
            label9.Text = "&Status : Success!!!";
            textBox3.Text = generateRandomID(Convert.ToInt32(textBox6.Text));

            if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
            {
                listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
            }
            webBrowser2.Navigate(textBox4.Text);
        }
    }

在我的程序中有两个listBox,其中listBox包含显示在执行每个documentcomplete事件期间创建的id和密码,第二个listbox包含必须在webbrowser控件中打开的注册链接。

除了v​​alidation()方法中的条件成功之外,每件事都可以正常工作。

当程序执行并且用户开始注册过程时,如果用户成功创建了ID,则列表框项目将被触发两次。也许是因为如果用户成功注册,他被重定向到另一页,导致文件完成事件的复杂化。

我在某个地方遗漏了一些东西,尝试了很多,任何帮助都会受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

@sauk,  如果文档事件无法正确完成,它将保持交互状态,因此您可以使用

private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {  
       // check event state is complete or interactive
        if (webBrowser2.ReadyState != WebBrowserReadyState.Complete)
             return;
            validation();
            captcha();

         }

您的函数(validation()和captcha())仅在文档完成事件完成时调用。否则它不会调用你的函数