title: C# WPF - Waiting for page to load
body: <p>I have seen several threads on StackOverflow concerning this topic, however none of them seem to provide an answer.</p>
  
  <p>I have a button that, when clicked, opens up an invisible web page, navigates to a URL, enters information into a box, presses a button, and then scrapes the screen for information.</p>
  
  <p>The bones of my code basically in the click:</p>
  
  <pre><code>WebBrowser wb = new WebBrowser;
  wb.Visibility = System.Windows.Visibility.Hidden;
  wb.Navigate("http://somepage.com");
  </code></pre>
  
  <p>And this is where it gets tricky.</p>
  
  <p>I am looking for a way to <strong>ensure</strong> that the page is loaded before trying to enter data or scrape the screen. I have seen several threads that talk about <code>Navigated</code>, <code>IsLoaded</code>, <code>LoadCompleted</code> as well as <code>BackgroundWork</code> stuff, but I cannot get any of these to work. </p>
  
  <p>Which is the best option to use to determine that the page has fully loaded? How would you get the chosen method to work?</p>
  
  <p>I also cannot get the data from the screen as WPF does not use the same <code>GetElementByID</code>.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Per the comment below, here are the errors I run into:</p>
  
  <ul>
  <li>Navigated first as soon as the page has been navigated too and does not necessarily wait until all objects are loaded. This works as intended, but cannot be used for my purposes.</li>
  <li><p><code>IsLoaded</code> never returns true</p>
  
  <pre><code>private void GetData_Click(object sender, RoutedEventArgs e)
  {
    int x=0;
    HTMLDocument doc;
  
    wb = new WebBrowser();
    wb.Visibility = System.Windows.Visibility.Visible;
    wb.Navigate("somesite.com");
  
    doc = wb.Document as mshtml.HTMLDocument;
  
    while(!wb.IsLoaded)
    {
      //Wait
    }
  
    doc.getElementById("txt_One").innerText = "It Worked";
  
  }
  </code></pre></li>
  </ul>
  
  <p>Puts it in an infinite loop as <code>wb</code> does not ever seem to load.</p>
  
  <ul>
  <li>This is the version with LoadCompleted</li>
  </ul>
  
  <blockquote>
    <p>The event 'System.Windows.Controls.WebBrowser.LoadCompleted' can only appear on the left hand side of += or -=</p>
  </blockquote>
  
  <pre><code>    private void GetData_Click(object sender, RoutedEventArgs e)
      {
        int x=0;
        HTMLDocument doc;
  
        wb = new WebBrowser();
        wb.Visibility = System.Windows.Visibility.Visible;
        wb.Navigate("somesite.com");
  
        doc = wb.Document as mshtml.HTMLDocument;
  
        wb.LoadCompleted += wb_LoadCompleted;
  
        doc.getElementById("txt_One").innerText = "It Worked";
  
      }
  
      void wb_LoadCompleted(object sender, NavigationEventArgs e)
      {
  
      }
  </code></pre>
  
  <p>Produces the error</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.NullReferenceException' occured in {filename}</p>
    
    <p>Additional information: Object reference not set to an instance of an object.</p>
  </blockquote>