【问题标题】:How to get HTML from WebBrowser control [duplicate]如何从 WebBrowser 控件获取 HTML [重复]
【发布时间】:2017-05-11 08:54:52
【问题描述】:

有很多类似的帖子。

How to get rendered html (processed by Javascript) in WebBrowser control? 建议使用类似

webBrowser1.Document.GetElementsByTagName("HTML")[0].OuterHtml;

Document 被视为对象,我没有选择使用GetElementsByTagName

Copy all text from webbrowser control 建议使用DocumentText

我有Document,但没有DocumentText

该帖子还建议webBrowser.Document.Body.InnerText;

我可以选择使用webBrowser.Document,但仅此而已。出于某种原因,webBrowser.Document 是一个对象,因此我无法访问这些方法。

Getting the HTML source through the WebBrowser control in C# 也建议使用DocumentStream。再说一次,我没有那个。

我在 WPF 应用程序中执行此操作,并使用来自 System.Windows.ControlsWebBrowser

我只想从网页中读取呈现的 HTML。

我的代码

public void Begin(WebBrowser wb)
{
   this._wb = wb;
   _wb.Navigated += _wb_Navigated;
   _wb.Navigate("myUrl");
}

private void _wb_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    var html = _wb.Document;//this is where I need help
}

【问题讨论】:

  • _wb.DocumentText ?
  • @JuryGolubev,看看我帖子的第 6 行。我已经解释过它在智能感知中不存在一个选项:)
  • @Equalsk,实际上,这可以被标记为骗子。抱歉,我花了 2 天时间寻找并没有看到那个帖子
  • 你的用户名是合适的 ;-)

标签: c# html wpf


【解决方案1】:

您的示例参考WinForms-WebBrowserControl。 向您的项目添加对Microsoft.mshtml 的引用(通过添加引用对话框->搜索)。

Document-属性转换为

HTML文档

为了访问方法和属性(如 MSDN 所述)。

另见我的GitHub-Sample

private void WebBrowser_Navigated(object sender, NavigationEventArgs e) {
    var document = (HTMLDocument)_Browser.Document;
     _Html.Text = document.body.outerHTML;
}

【讨论】:

  • 如果你不需要在屏幕上实际渲染 html,你最好使用WebClient
猜你喜欢
  • 2011-09-02
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
  • 2011-11-12
相关资源
最近更新 更多