C# WebBrowser 按钮单击,然后转到另一个页面
我需要单击 html 按钮并导航到另一个页面。单击后我需要等待页面加载,只有当旧页面加载时才转到新页面。
这是单击按钮的代码:
element = webBrowser1.Document.GetElementById("LoginButton");
element.InvokeMember("click");
webBrowser 有一个 IsBusy 属性,但单击按钮后不起作用:
element = webBrowser1.Document.GetElementById("LoginButton");
element.InvokeMember("click");
if(webBrowser1.IsBusy)
{
MessageBox.Show("Busy"); // Nothing happens, but page is not full loaded.
}
如果我添加 System.Threading.Thread.Sleep(1000)
页面将加载我可以转到下一页,但其他计算机上的页面加载时间可能会更长。
如何才能仅在加载上一个页面后才加载另一个页面?
PS:我来自俄罗斯,很抱歉英语不好。
I need to click an html button and navigate to another page. After click I need to wait for page loading, and go to the new page only when the old page loaded.
Here is the code, that click a button:
element = webBrowser1.Document.GetElementById("LoginButton");
element.InvokeMember("click");
webBrowser has got a IsBusy property, but it don`t works after button click:
element = webBrowser1.Document.GetElementById("LoginButton");
element.InvokeMember("click");
if(webBrowser1.IsBusy)
{
MessageBox.Show("Busy"); // Nothing happens, but page is not full loaded.
}
If I add System.Threading.Thread.Sleep(1000)
the page loads and I can go to next page, but page loading time on other computers can be more.
What can I do to load another page only after the previous page has loaded?
P.S: I am from Russia, so sorry for bad English.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的网页有任何 javascript 块,您将无法使用 WebBrowser 控件本身解决问题。您应该使用 javascript 代码等待 document.ready 事件,并让您的 C# 程序了解该事件。
之前,我制作了一个提供网页状态的 JavaScript 块。它看起来像这样:
以及等待它返回 true 的 C# 代码:
If your webpage has any javascript blocks, you won't be able to solve the problem using the WebBrowser control itself. You should wait for a document.ready event using javascript code and let know your C# program about it.
Previously, I made a javascript block that provides the webpage state. It looks like this:
and a C# code that waits for it to return true:
WebBrowser
控件公开了许多事件。您可以尝试Naviged
或DocumentCompleted
。缺口
There are lots of events exposed by the
WebBrowser
control. You might tryNavigated
orDocumentCompleted
.Nick
WebBrowser.Navierated
是 浏览器事件。WebBrowser.Navigated
is the browser event you are seeking.用这个,
您可能只能使用一次
调用
将 br1 替换为您的网络浏览器名称
希望这有帮助
Use this ,
You might just be able to use this once
Call
Replace br1 with your webbrowser name
Hope this helps