WebBrowser Navigating function does not work, and handlers are not called

The code is below.

I try to go to the website and read the information, the problem is that Navigate is not working, the only event that is being fired is "Navigation", and the printed Url is empty, other events are never fired. What am I missing? Do I need to use the Form class for navigation? Can I use it programmatically from a console application?

Please, help.

class WebNavigator { private readonly WebBrowser webBrowser; public WebNavigator() { webBrowser = new WebBrowser { AllowNavigation = true }; webBrowser.Navigated += webBrowser_Navigated; webBrowser.Navigating += webBrowser_Navigating; webBrowser.DocumentCompleted += webBrowser_DocumentCompleted; } // Navigates to the given URL if it is valid. public void Navigate(string address) { if (String.IsNullOrEmpty(address)) return; if (address.Equals("about:blank")) return; if (!address.StartsWith("http://") && !address.StartsWith("https://")) { address = "http://" + address; } try { Trace.TraceInformation("Navigate to {0}", address); webBrowser.Navigate(new Uri(address)); } catch (System.UriFormatException) { Trace.TraceError("Error"); return; } } // Occurs when the WebBrowser control has navigated to a new document and has begun loading it. private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e) { Trace.TraceInformation("Navigated to {0}", webBrowser.Url); } // Occurs before the WebBrowser control navigates to a new document. private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e) { Trace.TraceInformation("Navigating to {0}", webBrowser.Url); } private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { var wb = sender as WebBrowser; Trace.TraceInformation("DocumentCompleted {0}", wb.Url); } } 
+3
c # webbrowser-control
Aug 25 '13 at 11:44
source share
3 answers

I would use wait / asynchronous search .

Using:

 static async void DoWork() { var html = await GetHtmlAsync("http://www.google.com/"); } 

Utility method

 static Task<string> GetHtmlAsync(string url) { var tcs = new TaskCompletionSource<string>(); var thread = new Thread(() => { WebBrowser wb = new WebBrowser(); WebBrowserDocumentCompletedEventHandler documentCompleted = null; documentCompleted = async (o, s) => { wb.DocumentCompleted -= documentCompleted; await Task.Delay(2000); //Run JS a few seconds more tcs.TrySetResult(wb.DocumentText); wb.Dispose(); Application.ExitThread(); }; wb.ScriptErrorsSuppressed = true; wb.DocumentCompleted += documentCompleted; wb.Navigate(url); Application.Run(); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); return tcs.Task; } 
+6
Aug 25 '13 at 12:55
source share

You mentioned that this is a console application. For WebBrowser work in a console application, you need to make sure that the browser stream is an STA stream ( thread.SetApartmentState(ApartmentState.STA ). In addition, to process events, the stream must pump messages ( Application.Run ). Given this, you can create a separate thread (from the main console thread) to launch WebBrowser.

+1
Aug 25 '13 at
source share

Thanks for answers. The problem was resolved by adding this while clause, including Application.DoEvents ();

  webBrowser.Navigate(new Uri(address)); while (!navigationCompleted) { Application.DoEvents(); Thread.Sleep(navigationPollInterval); } 

I am not sure if this is the best solution, I would appreciate a better solution.

+1
Aug 25 '13 at 12:52
source share



All Articles