wpf 如何从 WebBrowser 控件获取 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43910837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:05:14  来源:igfitidea点击:

How to get HTML from WebBrowser control

c#htmlwpf

提问by MyDaftQuestions

There are loads of posts similar to this.

有很多类似的帖子。

How to get rendered html (processed by Javascript) in WebBrowser control?suggests to use something like

如何在 WebBrowser 控件中获取呈现的 html(由 Javascript 处理)?建议使用类似的东西

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

Documentis treated as an object, I have no option to use GetElementsByTagName

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

Copy all text from webbrowser controlsuggests to use DocumentText

从 webbrowser 控件复制所有文本建议使用DocumentText

I have Documentbut no DocumentText

我有Document但没有DocumentText

That post also suggests webBrowser.Document.Body.InnerText;

那个帖子还暗示 webBrowser.Document.Body.InnerText;

I have the option to use webBrowser.Documentbut that is it. For some reason webBrowser.Documentis an object and as such I can't access these methods.

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

Getting the HTML source through the WebBrowser control in C#also suggests using DocumentStream. Again, I don't have that.

通过 C# 中的 WebBrowser 控件获取 HTML 源代码也建议使用DocumentStream. 再说一次,我没有那个。

I'm doing this in a WPF application and using WebBrowserfrom System.Windows.Controls

我在 WPF 应用程序中执行此操作并使用WebBrowserfromSystem.Windows.Controls

All I'm trying to is read the rendered HTML from the web page.

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

My code

我的代码

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
}

采纳答案by earloc

Your samples refer to the WinForms-WebBrowserControl. Add a reference to Microsoft.mshtml(via add-reference dialog->search) to your project.

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

Cast the Document-Property to

Document-Property投射到

HTMLDocument

HTML文档

in order to access methods and properties (as stated on MSDN).

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

See also my GitHub-Sample:

另请参阅我的GitHub-Sample

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