等到加载Web浏览器以执行代码?

时间:2013-06-09 05:21:22

标签: c# browser

我有这段代码:

private void start_Click(object sender, EventArgs e)
    {
        var webBrowsers = new [] { web1, web2, web3, web4 };
        for (int i = 0; i < webBrowsers.Length; i++)
        {
                status.Text = "Status: Creating account " + (on + 1).ToString();
                webBrowsers[i].Document.GetElementById("user[name]").SetAttribute("value", "Steven John");
                webBrowsers[i].Document.GetElementById("user[email]").SetAttribute("value", "StevenJohn" + on.ToString() + "@gmail.com");
                webBrowsers[i].Document.GetElementById("user[user_password]").SetAttribute("value", "froots2");
                webBrowsers[i].Document.GetElementById("user[screen_name]").SetAttribute("value", "StevenJohn" + on.ToString());
                webBrowsers[i].Document.Forms[0].InvokeMember("submit");
                on += 1;
                acct = System.IO.File.ReadAllText(@"C:\Users\CMN573\twitter\made.txt");
                System.IO.File.WriteAllText(@"C:\Users\CMN573\twitter\made.txt", acct + Environment.NewLine + "stevenjohn" + on.ToString());
                System.IO.File.WriteAllText(@"C:\Users\CMN573\twitter\number.txt", on.ToString());
        }
    }

我想等到页面完成后再执行整个代码。我在这里找到的其他问题对我没有帮助,因为在我的代码放在哪里有点令人困惑。当我按下按钮时,我需要这样做。

1 个答案:

答案 0 :(得分:0)

如果你找到了,你想在一个网站注册4个帐户,而不是使用4个网络浏览器,你可以这样做:

public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate("To your register account url");
        }
        int on = 0;
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (on <= 4)
            {
                //status.Text = "Status: Creating account " + (on + 1).ToString();
                webBrowser1.Document.GetElementById("user[name]").SetAttribute("value", "Steven John");
                webBrowser1.Document.GetElementById("user[email]").SetAttribute("value", "StevenJohn" + on.ToString() + "@gmail.com");
                webBrowser1.Document.GetElementById("user[user_password]").SetAttribute("value", "froots2");
                webBrowser1.Document.GetElementById("user[screen_name]").SetAttribute("value", "StevenJohn" + on.ToString());
                //Submit must be valid
                webBrowser1.Document.Forms[0].InvokeMember("submit");
                on += 1;
                string acct = System.IO.File.ReadAllText(@"C:\Users\CMN573\twitter\made.txt");
                System.IO.File.WriteAllText(@"C:\Users\CMN573\twitter\made.txt", acct + Environment.NewLine + "stevenjohn" + on.ToString());
                System.IO.File.WriteAllText(@"C:\Users\CMN573\twitter\number.txt", on.ToString());
                //Navigate to url again
                webBrowser1.Navigate("To your register account url");
            }