0

I have written some code to post messages to a site. It works just fine when I run it with button clicks, but if I try to run the whole thing with one single shot it throws an error. I know that the problem is that when I try to run it all in one shot the WebBrowser is not loading the page totally and thus it can't post the data. I know this is an easy fix, but I am stumped.

private void button1_Start_Click(object sender, EventArgs e)//this is just the Pseudocode
    {



        GetData();

        SendData();//If I eliminate this and just fire the SendData method with a button click, program works fine

    }


    private void GetData()
    {

        webBrowser1.Navigate(inputURLID);
    }



    private void SendData()//if I replace this with  button2_Post_Click it works fine
    {

        webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2_Subject.Text);//To (username)

        webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject

        webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
    }



    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

    }
4

1 回答 1

0

您无法运行SendData(),因为您的页面不加载元素,您必须等到页面加载completely:试试这个:

 private void button1_Start_Click(object sender, EventArgs e)//this is just the Pseudocode
        {
            GetData();
            button1_Start.Enable = false;
        }

        private void GetData()
        {
            webBrowser1.Navigate("inputURLID");
        }

        private void SendData()
        {
            webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2_Subject.Text);//To (username)
            webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject
            webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
            button1_Start.Enable = true;
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            SendData();
        }
于 2013-06-09T14:57:27.180 回答