2

I've got a WebBrowser control on my page. To fire some javascript on a page displayed in the browser control. and when i try to save my web page i got this error

String strHtml = _browser.Document.Body.OuterText;
File.WriteAllText("C:\saveme.html", strHtml);

and this line _browser.Document.Body.OuterText throws the exception

Exception:: Specified cast is not valid.
at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()
at System.Windows.Forms.WebBrowser.get_Document()....

i had also used Invoke but does't work , is there any thing i missed out

itsho
  • 4,640
  • 3
  • 46
  • 68
Sarath Vuyyuru
  • 118
  • 1
  • 12

1 Answers1

5

Are you accessing the WebBrowser control from the UI thread? If not, try to access it from the UI thread or use Dispatcher.Invoke. See "Specified cast is not valid" error for more details.

Otherwise, the document probably has not completed loading yet. Wait until the DocumentCompleted event has fired to ensure the document has loaded, then access the property. See WebBrowser.Document.Body is always null for more information.

Community
  • 1
  • 1
akton
  • 14,148
  • 3
  • 43
  • 47