Timer定时调用webbrowser,浏览网页,但无法获取元素,无法点击
不使用定时器,使用webbrowser,浏览网页,获取元素和点击都是很正常但在定时器中,使用webbrowser,浏览网页,但无法获取元素,无法点击
源码
public Form1()
{
InitializeComponent();
ReadTimer = new System.Timers.Timer();
ReadTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
ReadTimer.Enabled = false;
webBrowser1.DocumentCompleted +=
new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser_DocumentCompleted);
}
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url == webBrowser1.Document.Url)
{
// 加载完毕
bComplete = true;
}
return;
}
private void g600000ToolStripMenuItem_Click(object sender, EventArgs e)
{
bComplete = false;
webBrowser1.Navigate("http://www.google.com", false);
while (!bComplete)//等页面加载结束
{
Application.DoEvents();
}
HtmlElement inputE = webBrowser1.Document.GetElementById("lst-ib");
inputE.SetAttribute("value", "600000");//google input
HtmlElement butoonE = webBrowser1.Document.GetElementById("btnK");
bComplete = false;
butoonE.InvokeMember("click");//click search
while (!bComplete)
{
Application.DoEvents();
}
}
private void 定时ToolStripMenuItem1_Click(object sender, EventArgs e)
{
ReadTimer.Interval = 30 * 1000;//毫秒单位,默认100 毫秒
ReadTimer.Enabled = true;
}
private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
ReadTimer.Stop();
bComplete = false;
webBrowser1.Navigate("http://www.google.com", false);
while (!bComplete)//等页面加载结束
{
Application.DoEvents();
}
HtmlElement inputE = webBrowser1.Document.GetElementById("lst-ib");
inputE.SetAttribute("value", "600000");//google input
HtmlElement butoonE = webBrowser1.Document.GetElementById("btnK");
bComplete = false;
butoonE.InvokeMember("click");//click search
while (!bComplete)
{
Application.DoEvents();
}
ReadTimer.Start();
}
单独调用g600000ToolStripMenuItem_Click很正常
但使用定时ToolStripMenuItem1_Click,定时GOOGLE,600000时,只会停在GOOGLE页面
跟踪发现,会停在语句
HtmlElement inputE = webBrowser1.Document.GetElementById("lst-ib");
跪求大拿指点。呵呵分不多,穷人:)
补充:.NET技术 , C#