以下是我的代码
public Form1()
{
InitializeComponent();
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.Navigate("http://www.somewebsite.com/login");
HtmlDocument doc = webBrowser1.Document;
doc.GetElementById("login").SetAttribute("value", "mylogin");
doc.GetElementById("pass").SetAttribute("value", "123456");
doc.GetElementById("submit").InvokeMember("click");
webBrowser1.Refresh();
webBrowser1.Navigate("http://www.somewebsite.com/newpage");
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
HtmlDocument doc = webBrowser1.Document;
doc.GetElementById("title").SetAttribute("value", textBox1.Text);
doc.GetElementById("content").SetAttribute("value", textBox2.Text);
doc.GetElementById("new-format").InvokeMember("click");
doc.GetElementById("check").InvokeMember("click");
webBrowser1.Document.GetElementById("mm").SetAttribute("value", "01");
webBrowser1.Document.GetElementById("jj").SetAttribute("value", "01");
webBrowser1.Document.GetElementById("aa").SetAttribute("value", "2013");
webBrowser1.Document.GetElementById("hh").SetAttribute("value", "01");
webBrowser1.Document.GetElementById("mm").SetAttribute("value", "01");
doc.GetElementById("publish").InvokeMember("click");
MessageBox.Show("Posted");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url != webBrowser1.Url)
return;
}
问题是我搜索了很多,并没有找到任何适当或真实的方式等待浏览器完成加载页面。
所以,我的问题是如何等到浏览器加载页面。我的程序将导航很多以做一些工作,所以我需要最好的方法来处理这个问题? 请提出一些建议
谢谢
答案 0 :(得分:0)
使用documentCompleted事件。那是文档完全加载的时候。
添加活动:
webBrowser1.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(checkDocument);
检查文件:
private void checkDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument doc = webBrowser1.Document;
doc.GetElementById("title").SetAttribute("value", textBox1.Text);
doc.GetElementById("content").SetAttribute("value", textBox2.Text);
doc.GetElementById("new-format").InvokeMember("click");
doc.GetElementById("check").InvokeMember("click");
webBrowser1.Document.GetElementById("mm").SetAttribute("value", "01");
webBrowser1.Document.GetElementById("jj").SetAttribute("value", "01");
webBrowser1.Document.GetElementById("aa").SetAttribute("value", "2013");
webBrowser1.Document.GetElementById("hh").SetAttribute("value", "01");
webBrowser1.Document.GetElementById("mm").SetAttribute("value", "01");
doc.GetElementById("publish").InvokeMember("click");
MessageBox.Show("Posted");
}
答案 1 :(得分:0)
将此if语句添加到按钮单击事件
中if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
HtmlDocument doc = webBrowser1.Document;
doc.GetElementById("title").SetAttribute("value", textBox1.Text);
doc.GetElementById("content").SetAttribute("value", textBox2.Text);
doc.GetElementById("new-format").InvokeMember("click");
doc.GetElementById("check").InvokeMember("click");
webBrowser1.Document.GetElementById("mm").SetAttribute("value", "01");
webBrowser1.Document.GetElementById("jj").SetAttribute("value", "01");
webBrowser1.Document.GetElementById("aa").SetAttribute("value", "2013");
webBrowser1.Document.GetElementById("hh").SetAttribute("value", "01");
webBrowser1.Document.GetElementById("mm").SetAttribute("value", "01");
doc.GetElementById("publish").InvokeMember("click");
MessageBox.Show("Posted");
}