gpt4 book ai didi

c# - 等到页面加载到 webbrowser 控件中

转载 作者:行者123 更新时间:2023-11-30 14:00:40 26 4
gpt4 key购买 nike

我正在使用 C# WebBrowswer 控件,但我遇到一个问题,即当页面尚未加载时按下“下一步”之类的按钮时,程序会尝试继续,但会给我一个空错误。

是否有让程序等待页面加载完成的函数?

我试图在检查 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();
}

最佳答案

你需要 Hook WebBrowswer.DocumentCompleted事件:

可能在您的构造函数或您的 OnLoad 中:

webBrowser1.Document.GetElementById("q").SetAttribute("value", "חחח"); 
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
webBrowser1.Document.GetElementById("btnK").InvokeMember("Click");

然后你的事件看起来像这样:

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// do the work you need to do now that that page has completed loading
}

关于c# - 等到页面加载到 webbrowser 控件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10166613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com