如何使用多个WebBrowser DocumentCompleted

时间:2017-01-23 22:32:52

标签: c# winforms webbrowser-control

我希望能够打开网页,登录,并能够在下一页上执行操作。 DoPlans不会执行。到目前为止,这是我的代码...(我知道我有很多变量)

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        webBrowser1.Navigate("t");
    }

    private void DoPlans(object sender,
WebBrowserDocumentCompletedEventArgs e)
    {
        textBox1.AppendText("started");
        webBrowser1.Document.GetElementById("t").InvokeMember("Click");
        webBrowser1.Document.GetElementById("t").InvokeMember("Click");
        foreach (HtmlElement elem in webBrowser1.Document.GetElementsByTagName("input"))
        {
            if (elem.GetAttribute("value") == "Submit Weekend Plans")
            {
                elem.InvokeMember("Click");
            }
        }
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        webBrowser1.Document.GetElementById("username").SetAttribute("value", "t");
        webBrowser1.Document.GetElementById("password").SetAttribute("value", "t");
        foreach (HtmlElement elem in webBrowser1.Document.GetElementsByTagName("input"))
        {
            if (elem.GetAttribute("value") == "Login")
            {
                webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DoPlans);
                elem.InvokeMember("Click");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    switch (e.Url.ToString())
    {
        case "home page":
        {
            // fire click
            break;
        }
        case "next page":
        {
            // handle logged in user
            break;
        }
    }
}