0

I have for plus 10 hours now tried to get a hole through to the WebBrowser property.

I am simply trying to navigate to google.com and print the html code to the console. This constantly gives me a null reference.

There is alot of questions related to this and they all say that a DocumentCompleted Eventhandler is needed or not setup properly.

So i tried this to the best of my ability though still no luck. I then went so far as to copy paste the official example from Microsoft I am still getting the null reference!

    using System;
    using System.Windows.Forms;


    namespace Aamanss
    {
        class MainClass
        {
            public static void PrintHelpPage()
            {
                // Create a WebBrowser instance. 
                WebBrowser webBrowserForPrinting = new WebBrowser();

                // Add an event handler that prints the document after it loads.
                webBrowserForPrinting.DocumentCompleted +=
                    new WebBrowserDocumentCompletedEventHandler(PrintDocument);

                // Set the Url property to load the document.
                webBrowserForPrinting.Url = new Uri("https://www.google.com");
            }

            public static void PrintDocument(object sender,
                WebBrowserDocumentCompletedEventArgs e)
            {
                // Print the document now that it is fully loaded.
                ((WebBrowser)sender).Print();

                // Dispose the WebBrowser now that the task is complete. 
                ((WebBrowser)sender).Dispose();
            }

            public static void Main(string[] args)
            {
                PrintHelpPage();
            }
        }

}

As you can see the majority of the above code is copy pasted from Microsoft. And this errors:

Gtk-Message: Failed to load module "atk-bridge"
libgluezilla not found. To have webbrowser support, you need libgluezilla installed        
System.NullReferenceException: Object reference not set to an instance of an object
      at System.Windows.Forms.WebBrowser.Navigate (System.Uri url) [0x0000e] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:304 
      at System.Windows.Forms.WebBrowser.set_Url (System.Uri value) [0x00007] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:231 
      at (wrapper remoting-invoke-with-check) System.Windows.Forms.WebBrowser:set_Url (System.Uri)
      at Aamanss.MainClass.PrintHelpPage () [0x00024] in /root/Projects/Aamanss/Aamanss/Program.cs:19 
      at Aamanss.MainClass.Main (System.String[] args) [0x00001] in /root/Projects/Aamanss/Aamanss/Program.cs:34 
    [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
      at System.Windows.Forms.WebBrowser.Navigate (System.Uri url) [0x0000e] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:304 
      at System.Windows.Forms.WebBrowser.set_Url (System.Uri value) [0x00007] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:231 
      at (wrapper remoting-invoke-with-check) System.Windows.Forms.WebBrowser:set_Url (System.Uri)
      at Aamanss.MainClass.PrintHelpPage () [0x00024] in /root/Projects/Aamanss/Aamanss/Program.cs:19 
      at Aamanss.MainClass.Main (System.String[] args) [0x00001] in /root/Projects/Aamanss/Aamanss/Program.cs:34

I am really desperate to figure out how you make a WebBrowser Navigation work in general and therefore i sincerely hope one of you can illustrate this for dummies.

Nulle
  • 1,301
  • 13
  • 28
  • are you printing to print to console or to a physical printer. – Digvijay Apr 20 '17 at 11:09
  • I am printing to the console. I am running it in monoDevelop as a console app – Nulle Apr 20 '17 at 11:09
  • Check this answer: http://stackoverflow.com/questions/19734151/print-webbrowser-without-previewing-i-e-single-click-print – Zsmaster Apr 20 '17 at 11:20
  • Do you specifically want to use the WebBrowser control for this? There are easier ways of printing the google.com html to the console (e.g. `curl www.google.com`), and easier ways of retrieving the HTML using c# (`HttpWebRequest` class) – paul Apr 20 '17 at 13:28
  • I know but Paul but somehow I Find WebBrowser simpler to work with when it comes to get through a login form on a web. My ultimate goal is to login to a web and copy the response html into a string. – Nulle Apr 20 '17 at 14:46

1 Answers1

3

OK,

I misunderstood the problem that its a windows application (Winforms)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintHelpPage();
            Console.ReadKey();
        }

        public static void PrintHelpPage()
        {
            var th = new Thread(() => {
                var br = new WebBrowser();
                br.DocumentCompleted += PrintDocument;
                br.Navigate("http://google.com");
                Application.Run();
            });
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
        }

        public static void PrintDocument(object sender,
            WebBrowserDocumentCompletedEventArgs e)
        {
            var browser = sender as WebBrowser;
            // Print the document now that it is fully loaded.
            browser.Print();

            // Dispose the WebBrowser now that the task is complete. 
            browser.Dispose();
        }
    }
}

the issue is that a console application would not fire the DocumentCompletedEvent unless you explicitly mark it STAThread as i did in the thread.

Digvijay
  • 774
  • 5
  • 10
  • I am still getting a null reference. – Nulle Apr 20 '17 at 11:15
  • I am sorry I am still getting a null reference. Have you tested this yourself? – Nulle Apr 20 '17 at 11:31
  • yes, did you check the line // Set the Url property to load the document. webBrowserForPrinting.Navigate("https://www.google.com"); – Digvijay Apr 20 '17 at 11:43
  • Here is a pastebin of how my code looks like now. Still getting a null reference. https://pastebin.com/jEAnh04Y – Nulle Apr 20 '17 at 11:52
  • all this while i was under assumption that this code fragment is part of a winforms app. Updated the answer with functional code now! – Digvijay Apr 20 '17 at 11:54
  • This code still gives me a null reference. But it just cam to my attention that this might not be a code issue but a software issue. I came to scroll a bit in the error and found: Gtk-Message: Failed to load module "atk-bridge" libgluezilla not found. To have webbrowser support, you need libgluezilla installed – Nulle Apr 20 '17 at 12:04
  • 1
    I am trying this in visual studio and not monoDevelop but i believe the principle would be the same!. I would not comment more or it would look like a chat :). I hope i could be of help! – Digvijay Apr 20 '17 at 12:06
  • btw. Installed Visual Studio and now all works like a charm. – Nulle Apr 20 '17 at 17:05