我已经编写了一些代码来向网站发布消息。当我按下按钮点击它时,它工作正常,但如果我尝试用一次单击运行整个事件就会抛出错误。我知道问题是,当我尝试一次性运行它时,WebBrowser不会完全加载页面,因此无法发布数据。我知道这很容易解决,但我很难过。
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)
{
}
答案 0 :(得分: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();
}