我正在使用 C# WebBrowser 控件,我遇到了一个问题,即当页面尚未加载但程序尝试继续但给我一个空错误时,当按下“下一步”之类的按钮时。
是否有让程序等待页面完成加载的功能?
我试图在检查html页面标题的程序中放置一个while循环,但随后程序冻结。类似的东西会冻结程序:
while(!webbrowser1.Document.Title.ToString().Equals("NextPageTitle"))
{
}
::NextCommands::
它不起作用,我试过了,但没有点击按钮“fblqf”。但它不返回空错误..
public void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("q").SetAttribute("value", "חחח");
webBrowser1.Document.GetElementById("btnK").InvokeMember("Click");
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// do the work you need to do now that that page has completed loading
webBrowser1.Document.GetElementById("fblqf").InvokeMember("Click");
}
解决方案:
public void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("q").SetAttribute("value", "חחח");
webBrowser1.Document.GetElementById("btnK").InvokeMember("Click");
int x=0;
while (x==0)
{
System.Windows.Forms.Application.DoEvents();
if(webBrowser1.Document.GetElementById("pnnext") != null)
break;
}
webBrowser1.Document.GetElementById("pnnext").InvokeMember("Click");
webBrowser1.Document.GetElementById("q").Focus();
}