我正在尝试创建一个程序,通过我们的缺陷跟踪系统的网络界面一次克隆多个错误。如何等待页面完全加载后再继续?
//This is pseudo code, but this should give you an idea of what I'm trying to do. The
//actual code uses multi-threading and all that good stuff :).
foreach (string bug in bugs)
{
webBrowser.Navigate(new Uri(url));
webBrowser.Document.GetElementById("product").SetAttribute("value", product);
webBrowser.Document.GetElementById("version").SetAttribute("value", version);
webBrowser.Document.GetElementById("commit").InvokeMember("click");
//Need code to wait for page to load before continuing.
}
尝试
DocumentCompleted
活动:
webBrowser.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser.Document.GetElementById("product").SetAttribute("value", product);
webBrowser.Document.GetElementById("version").SetAttribute("value", version);
webBrowser.Document.GetElementById("commit").InvokeMember("click");
}
这段代码对我非常有帮助。也许它也适合你
wb.Navigate(url);
while(wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
MessageBox.Show("Loaded");
在不阻塞 UI 线程的情况下执行此操作的最佳方法是使用 .net 4.5 中引入的 Async 和 Await。
您可以将其粘贴到代码中,只需将浏览器更改为您的网络浏览器名称即可。
这样,您的线程等待要加载的页面,如果没有按时加载,它将停止等待并且您的代码继续运行:
private async Task PageLoad(int TimeOut)
{
TaskCompletionSource<bool> PageLoaded = null;
PageLoaded = new TaskCompletionSource<bool>();
int TimeElapsed = 0;
Browser.DocumentCompleted += (s, e) =>
{
if (Browser.ReadyState != WebBrowserReadyState.Complete) return;
if (PageLoaded.Task.IsCompleted) return; PageLoaded.SetResult(true);
};
//
while (PageLoaded.Task.Status != TaskStatus.RanToCompletion)
{
await Task.Delay(10);//interval of 10 ms worked good for me
TimeElapsed++;
if (TimeElapsed >= TimeOut * 100) PageLoaded.TrySetResult(true);
}
}
您可以像这样使用它,在异步方法中,或者在按钮单击事件中,只需将其设为异步即可:
private async void Button1_Click(object sender, EventArgs e)
{
Browser.Navigate("www.example.com");
await PageLoad(10);//await for page to load, timeout 10 seconds.
//your code will run after the page loaded or timeout.
}
Task 方法对我有用,除了 Browser.Task.IsCompleted 必须更改为 PageLoaded.Task.IsCompleted。
抱歉我没有添加评论,那是因为我需要更高的声誉才能添加评论。
yuna 和 bnl 代码在以下情况下失败;
失败示例:
第一个等待完成。但是,第二个带有 invokemember("submit") 的没有。调用作品。但 ReadyState.Complete 的行为就像在真正完成之前已完成:
wb.Navigate(url);
while(wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
MessageBox.Show("ok this waits Complete");
//navigates to new page
wb.Document.GetElementById("formId").InvokeMember("submit");
while(wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
MessageBox.Show("webBrowser havent navigated yet. it gave me previous page's html.");
var html = wb.Document.GetElementsByTagName("HTML")[0].OuterHtml;
如何解决这种不需要的情况:
使用方法
public myForm1 {
myForm1_load() { }
// func to make browser wait is inside the Extended class More tidy.
WebBrowserEX wbEX = new WebBrowserEX();
button1_click(){
wbEX.Navigate("site1.com");
wbEX.waitWebBrowserToComplete(wb);
wbEX.Document.GetElementById("input1").SetAttribute("Value", "hello");
//submit does navigation
wbEX.Document.GetElementById("formid").InvokeMember("submit");
wbEX.waitWebBrowserToComplete(wb);
// this actually waits for document Compelete. worked for me.
var processedHtml = wbEX.Document.GetElementsByTagName("HTML")[0].OuterHtml;
var rawHtml = wbEX.DocumentText;
}
}
//put this extended class in your code.
//(ie right below form class, or seperate cs file doesnt matter)
public class WebBrowserEX : WebBrowser
{
//ctor
WebBrowserEX()
{
//wired aumatically here. we dont need to worry our sweet brain.
this.DocumentCompleted += (o, e) => { webbrowserDocumentCompleted = true;};
}
//instead of checking readState, get state from DocumentCompleted Event
// via bool value
bool webbrowserDocumentCompleted = false;
public void waitWebBrowserToComplete()
{
while (!webbrowserDocumentCompleted )
{ Application.DoEvents(); }
webbrowserDocumentCompleted = false;
}
}
尝试一下 Selenium (http://seleniumhq.org) 或 WatiN (http://watin.sourceforge.net) 可以节省一些工作。
如果您使用的是 InternetExplorer.Application COM 对象,请检查 ReadyState 属性的值是否为 4。
我认为 WebBrowser 控件的 DocumentCompleted 事件应该可以让您到达您需要去的地方。
假设“commit”元素代表标准表单提交按钮,那么您可以将事件处理程序附加到 WebBrowsers Navigated 事件。
while (true)
{//ie is the WebBrowser object
if (ie.ReadyState == tagREADYSTATE.READYSTATE_COMPLETE)
{
break;
}
Thread.Sleep(500);
}
我用这种方式等待页面加载。
基于@YilDizGezen 的解决方案,但有所简化。
private async Task PageLoad(int TimeOut)
{
int TimeElapsed = 0;
webBrowser1.DocumentCompleted += (o, e) =>
{
TimeElapsed = TimeOut * 100;
};
while (TimeElapsed < (TimeOut * 100))
{
await Task.Delay(100);
TimeElapsed++;
}
}
同样可以使用:
await PageLoad(10);