0

My code is as following:

 protected void Page_Load(object sender, EventArgs e)
    {

        webBrowser1 = new WebBrowser();

        webBrowser1.Navigate("http://www.wine-searcher.com/wine-valuation-f.lml");


        webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);


    }



    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

        webBrowser1.Document.GetElementById("wn1_input").InnerText = "William Fevre Les Clos, Chablis Grand Cru, France";
        webBrowser1.Document.GetElementById("vt1_input").InnerText = "NV";
        webBrowser1.Document.GetElementById("valButton").InvokeMember("click");
        TextBox1.Text= webBrowser1.Document.GetElementById("value1").InnerHtml;
    }

when I debug I get the nullreferenceException about my first line:

webBrowser1.Document.GetElementById("wn1_input").InnerText = "William Fevre Les Clos, Chablis Grand Cru, France";  

How can I resolve this please ?

Prophet
  • 32,350
  • 22
  • 54
  • 79
user3711521
  • 285
  • 5
  • 14
  • Try assigning webBrowser1.DocumentCompleted handler before navigating? – Yuriy Galanter Jun 05 '14 at 14:12
  • @yurly, if that was the problem, webbrowser1_DocumentCompleted would never get called. – Aheho Jun 05 '14 at 14:20
  • it's still raising null exception , It worked for me using windows form application with the same code,that's weird ! maybe because I added to my page:AutoEventWireup="true" and to my web conf : – user3711521 Jun 05 '14 at 14:23
  • Are you really trying to load a web browser control (Which afaik is mainly intended for WinForms apps?) on your server then browse to a page and I assume try to get some javascript onclick event to run and give you a result? If so, there has got to be a better way! – Richard Dalton Jun 05 '14 at 15:17

1 Answers1

1

You should not use the WebBrowser control on .NET. If you want to grab an Html page you have many options, like the WebClient class:

http://msdn.microsoft.com/en-us/library/fhd1f0sw(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/ms144202(v=vs.110).aspx

or HttpClient

http://msdn.microsoft.com/it-it/library/system.net.http.httpclient.aspx

to grab contents on an html document, you could give a try at a porting of the HtmlTidy library, such as http://sourceforge.net/projects/tidynet/

pomarc
  • 2,194
  • 3
  • 23
  • 30
  • On second thought, I would probably go with one of pomarc's recommendations. – TrevorBrooks Jun 05 '14 at 14:40
  • @user3711521 you can use, in alternative, the easier htmlagility pack, as in http://stackoverflow.com/questions/2113924/how-can-i-use-html-agility-pack-to-retrieve-all-the-images-from-a-website – pomarc Jun 12 '14 at 20:55