webbrowser.document throws指定的强制转换无效 [英] webbrowser.document throws Specified cast is not valid exception
问题描述
我的页面上有一个WebBrowser控件。在浏览器控件中显示的页面上触发一些JavaScript。当我尝试保存我的网页时,出现此错误
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);
这行_browser.Document.Body.OuterText引发异常
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()....
我也曾经使用过Invoke但不起作用,
有什么我错过的事情
i had also used Invoke but does't work , is there any thing i missed out
推荐答案
您是否从UI线程访问WebBrowser控件?如果不是,请尝试从UI线程访问它,或使用 Dispatcher.Invoke 一个>。请参阅>指定的转换无效。错误以获取更多详细信息。
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.
否则,文档可能尚未完成加载。等待直到 DocumentCompleted 事件发生触发以确保文档已加载,然后访问属性。有关更多信息,请参见 WebBrowser.Document.Body始终为空。
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.
这篇关于webbrowser.document throws指定的强制转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!