How to use WebBrowser control DocumentCompleted event in C#?
You might want to know the AJAX calls as well.
Consider using this:
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string url = e.Url.ToString();
if (!(url.StartsWith("http://") || url.StartsWith("https://")))
{
// in AJAX
}
if (e.Url.AbsolutePath != this.webBrowser.Url.AbsolutePath)
{
// IFRAME
}
else
{
// REAL DOCUMENT COMPLETE
}
}
WebBrowser Document Completed Event C#
As I recall, DocumentCompleted will fire multiple times if the document being navigated to has iframes that embed other web pages.
If you only want to receive the event exactly once, just unsubscribe from the DocumentCompleted handler:
public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webBrowser = sender as WebBrowser;
webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
MessageBox.Show(webBrowser.Url.ToString() );
}
private void navBtnClick(object sender, EventArgs e)
{
var wbrowser = new WebBrowser();
wbrowser.DocumentCompleted +=new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
wbrowser.Navigate("http://www.google.com");
}
Alternately, you can use System.IObservable and ReactiveExtensions to subscribe to exactly one event firing:
private void navBtnClick(object sender, EventArgs e)
{
var browser = new WebBrowser();
var docCompleted = Observable.FromEventPattern<WebBrowserDocumentCompletedEventArgs>(browser, "DocumentCompleted")
docCompleted
.Take(1) // Take only one event firing
.Subscribe(i => MessageBox.Show(browser.Url.ToString()));
browser.Navigate("http://www.google.com");
}
C# WebBrowser DocumentCompleted event not firing with specific site
As I recall, this can happen in certain scenarios with iframes and AJAX calls.
Have you tried something like this instead:
webBrowser.Navigate("http://www.elinformador.mx");
while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
// The page is done loading. Do whatever now.
Click button after webBrowser1_DocumentCompleted event
After finding that the addition of a MessageBox allows the webBrowser1.Document to complete, and using webBrowser1.ReadyState event within the webBrowser_DocumentCompleted event, all I needed to do, was to find a way to programmatically close the MessageBox.
Further searching on StackOverFlow revealed the following solution on the site below.
Close a MessageBox after several seconds
Implementing the AutoClosingMessageBox, and setting a time interval, closed the MessageBox and allowed my button click, i.e. btnMyUrl.PerformClick(); to successfully parse the OuterHtml and now the code works properly.
Hopefully, if someone else discovers that placing a MessageBox within the webBrowser_DocumentCompleted event allows the document to complete; the aforementioned AutoClosingMessageBox will assist them as well.
How to use multiple WebBrowser DocumentCompleted
Try something like this:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
switch (e.Url.ToString())
{
case "home page":
{
// fire click
break;
}
case "next page":
{
// handle logged in user
break;
}
}
}
Related Topics
C# Filestream:Optimal Buffer Size for Writing Large Files
How to Use Interface as a C# Generic Type Constraint
How to Do a Sha1 File Checksum in C#
How to Use C# to Sanitize Input on an HTML Page
How to Solve Operator '!=' Cannot Be Applied to Operands of Type 'T' and 'T'
How to Insert a Data Table into SQL Server Database Table
Populate Treeview with File System Directory Structure
How to Add a Custom Routed Command in Wpf
How to Check If Object Already Exists in a List
Run Code Once Before and After All Tests in Xunit.Net
How to Know User Has Clicked "X" or the "Close" Button
How to Embed an Application Manifest into an Application Using VS2008