使用WebBrowser控件发布消息
本文关键字:消息 布消息 WebBrowser 控件 使用 | 更新日期: 2023-09-27 18:16:52
我写了一些代码来发布消息到一个网站。当我点击按钮运行它时,它工作得很好,但如果我试图用一次射击来运行整个事情,它会抛出一个错误。我知道问题是,当我试图在一个镜头中运行它时,web浏览器没有完全加载页面,因此它无法发布数据。我知道这很容易解决,但我被难住了。
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)
{
}
你不能运行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();
}