【问题标题】:webbrowser.document throws Specified cast is not valid exceptionwebbrowser.document 抛出 Specified cast is not valid 异常
【发布时间】:2012-09-15 14:40:51
【问题描述】:

我的页面上有一个 WebBrowser 控件。在浏览器控件中显示的页面上触发一些 javascript。当我尝试保存我的网页时出现此错误

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

而 _browser.Document.Body.OuterText 这一行会抛出异常

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

我也使用过 Invoke 但不起作用, 有什么我错过的吗

【问题讨论】:

    标签: winforms c#-4.0


    【解决方案1】:

    您是否正在从 UI 线程访问 WebBrowser 控件?如果没有,请尝试从 UI 线程访问它或使用Dispatcher.Invoke。详情请见"Specified cast is not valid" error

    否则,文档可能尚未完成加载。等到DocumentCompleted 事件触发以确保文档已加载,然后访问该属性。请参阅WebBrowser.Document.Body is always null 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2020-06-05
      • 2020-05-26
      • 2013-03-12
      • 1970-01-01
      相关资源
      最近更新 更多