【发布时间】:2014-05-13 23:02:12
【问题描述】:
我正在创建一个 c# 窗口表单应用程序,用户可以在其中注册到我的网站。该程序旨在简化从桌面进行多次注册的过程。我有 2 个活动用于注册过程。
第一个事件:
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);
}
}
在我的程序中有两个列表框,其中列表框包含显示在每个文档完成事件执行期间创建的 ID 和密码,第二个列表框包含必须在 webbrowser 控件中打开的注册链接。
除了validation()方法的条件成功之外,一切都很好。
当程序被执行并且用户开始注册过程,并且如果用户成功地创建了一个ID,列表框项目被触发两次。可能是因为如果用户注册成功,他会被重定向到另一个页面,从而导致文档完成事件的复杂性。
我在某处遗漏了一些东西,尝试了很多,任何帮助将不胜感激。
【问题讨论】:
标签: c# listbox automation webbrowser-control