Search code examples
c#.netcontrolsbrowsernavigateurl

.NET C#: WebBrowser control Navigate() does not load targeted URL


I'm trying to programmatically load a web page via the WebBrowser control with the intent of testing the page & it's JavaScript functions. Basically, I want to compare the HTML & JavaScript run through this control against a known output to ascertain whether there is a problem.

However, I'm having trouble simply creating and navigating the WebBrowser control. The code below is intended to load the HtmlDocument into the WebBrowser.Document property:

WebBrowser wb = new WebBrowser();
wb.AllowNavigation = true;

wb.Navigate("http://www.google.com/");

When examining the web browser's state via Intellisense after Navigate() runs, the WebBrowser.ReadyState is 'Uninitialized', WebBrowser.Document = null, and it overall appears completely unaffected by my call.

On a contextual note, I'm running this control outside of a Windows form object: I do not need to load a window or actually look at the page. Requirements dictate the need to simply execute the page's JavaScript and examine the resultant HTML.

Any suggestions are greatly appreciated, thanks!


Solution

  • You should handle the WebBrowser.DocumentComplete event, once that event is raised you will have the Document etc.

    wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
    


    private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
      WebBrowser wb = sender as WebBrowser;
      // wb.Document is not null at this point
    }
    

    Here is a complete example, that I quickly did in a Windows Forms application and tested.

    public partial class Form1 : Form
      {
        public Form1()
        {      
          InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
          WebBrowser wb = new WebBrowser();
          wb.AllowNavigation = true;
    
          wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
    
          wb.Navigate("http://www.google.com");
    
                  }
    
        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
          WebBrowser wb = sender as WebBrowser;
          // wb.Document is not null at this point
        }
      }
    

    Edit: Here is a simple version of code that runs a window from a console application. You can of course go further and expose the events to the console code etc.

    using System;
    using System.Windows;
    using System.Windows.Forms;
    
    namespace ConsoleApplication1
    {
      class Program
      {    
        [STAThread] 
        static void Main(string[] args)
        {      
          Application.Run(new BrowserWindow());   
    
          Console.ReadKey();
        }
      }
    
      class BrowserWindow : Form
      {
        public BrowserWindow()
        {
          ShowInTaskbar = false;
          WindowState = FormWindowState.Minimized;
          Load += new EventHandler(Window_Load);
        }
    
        void Window_Load(object sender, EventArgs e)
        {      
          WebBrowser wb = new WebBrowser();
          wb.AllowNavigation = true;
          wb.DocumentCompleted += wb_DocumentCompleted;
          wb.Navigate("http://www.bing.com");      
        }
    
        void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
          Console.WriteLine("We have Bing");
        }
      }
    }