C#HtmlDocument完整的HTML

时间:2014-03-20 10:14:01

标签: c# html wpf webbrowser-control windows-forms-host

这是我的问题:我使用WebBrowser加载页面,更精确this页面。正如您所看到的,该页面有很多js脚本,可以动态地将样式加载到页面中(请注意所有样式标记)。在我的应用程序中,我看到页面原样(正确加载样式),但是当我尝试导出HMTL时:

documentText = (browser.Document.DomDocument as mshtml.IHTMLDocument3).documentElement.outerHTML;

我确实得到了页面的HTML,但样式标签是空的(这是一个问题)。如何获取带样式的HTML?

编辑:我忘了提到我正在使用Windows窗体浏览器但是它位于WPF中的Windows窗体主机中。简短的例子是:

private System.Windows.Forms.WebBrowser browser;
        public MainWindow()
        {
            SetFeatureBrowserEmulation();
            InitializeComponent();
            browser = new System.Windows.Forms.WebBrowser();
            browser.ScriptErrorsSuppressed = true;

            System.Windows.Forms.Integration.WindowsFormsHost host =
        new System.Windows.Forms.Integration.WindowsFormsHost();
            host.Child = browser;
            var control = new System.Windows.Controls.UserControl();
            control.Content = host;
            browser.DocumentCompleted += browser_DocumentCompleted;
            rootPanel.Children.Add(control);
            browser.Navigate("http://instagram.com/p/lhG0xdE8B4");
        }

        void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var html = this.browser.Document.GetElementsByTagName("head")[0].OuterHtml; //still getting empty script tags
        }

        static void SetFeatureBrowserEmulation()
        {
            if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
                return;
            var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
            Microsoft.Win32.Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
                appName, 9000, Microsoft.Win32.RegistryValueKind.DWord);
        }

我得到的是什么:

...<style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style><style type="text/css"></style>...

我应该得到什么:

...<style type="text/css">.VerticalCenter {
  display: table;
  height: 100%;
  width: 100%; }

.vcCell {
  display: table-cell;
  vertical-align: middle; }
</style><style type="text/css">.formActions input[type=submit], .formActions input[type=submit]:visited {
  color: #fff; }
.formActions input[type=submit]:hover {
  color: #fff; }
.formActions input.ButtonActive[type=submit], .formActions input.button-active[type=submit], .formActions input.active[type=submit], .formActions input[type=submit]:active, html.touch .formActions input.pressed[type=submit], .formActions html.touch input.pressed[type=submit] {
  color: rgba(255, 255, 255, 0.8);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3); }

.formActions input[type=submit] {
  background-color: #6bb38a;
  background-image: -webkit-gradient(linear, left top, left bottom, from(#6bb38a), to(#3d8b5f));
  background-image: -webkit-linear-gradient(top, #6bb38a, #3d8b5f);
  background-image: -moz-linear-gradient(top, #6bb38a, #3d8b5f);
  background-image: -o-linear-gradient(top, #6bb38a, #3d8b5f);
  background-image: -ms-linear-gradient(top, #6bb38a, #3d8b5f);
  background-image: linear-gradient(top, #6bb38a, #3d8b5f);
  filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0,StartColorStr='#6bb38a', EndColorStr='#3d8b5f');
  background-position: 50% 50%;
  text-shadow: 0 1px 1px #1e693f; }
  .formActions input.ButtonActive[type=submit], .formActions input.button-active[type=submit], .formActions input.active[type=submit], .formActions input[type=submit]:active, html.touch .formActions input.pressed[type=submit], .formActions html.touch input.pressed[type=submit] {
    background: #3a845a; }
  .formActions input[type=submit] {
    border: 1px solid #3d8b5f; }
  .formActions input.ButtonActive[type=submit], .formActions input.button-active[type=submit], .formActions input.active[type=submit], .formActions input[type=submit]:active, html.touch .formActions input.pressed[type=submit], .formActions html.touch input.pressed[type=submit] {
    border-color: #2d6847; }

.igModalDialog .igDialogInner,
.igModalDialog .igDialogHeader {
  border: none;
  margin: 0; }
.igModalDialog .igDialogHeader {
  -webkit-border-radius: 3px 3px 0 0;
  border-radius: 3px 3px 0 0; }

.igDialogLayer {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  padding: 16px;
  background-color: rgba(1, 1, 1, 0.725);
  z-index: 100;
  overflow: auto; }

.touch .igDialogLayer {
  cursor: pointer; }

.igDialogLayer.vcCell {
  padding: 16px; }

.igdContentWrap {
  padding: 8px 32px; }

.igDialogContent {
  margin: 0 auto;
  background: #fdfdfd;
  -webkit-border-radius: 4px;
  border-radius: 4px;
  -webkit-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
  -moz-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
  -webkit-background-clip: padding-box;
  background-clip: padding-box; }

.igDialogHeader {
  position: relative;
  height: 44px;
  border: 1px solid #1c5380;
  border-width: 1px 1px 0;
  -webkit-box-shadow: 0 -1px 0 #06365f inset, 0 1px 0 rgba(255,255,255,0.2) inset;
  -moz-box-shadow: 0 -1px 0 #06365f inset, 0 1px 0 rgba(255,255,255,0.2) inset;
  box-shadow: 0 -1px 0 #06365f inset, 0 1px 0 rgba(255,255,255,0.2) inset;
  -webkit-border-radius: 4px 4px 0 0;
  border-radius: 4px 4px 0 0;
  background-color: #517fa4;
  background-image: url(//d36xtkk24g8jdx.cloudfront.net/bluebar/b4d4e0e/images/shared/noise-1.png), -webkit-gradient(linear, left top, left bottom, from(#517fa4), to(#306088));
  background-image: url(//d36xtkk24g8jdx.cloudfront.net/bluebar/b4d4e0e/images/shared/noise-1.png), -webkit-linear-gradient(top, #517fa4, #306088);
  background-image: url(//d36xtkk24g8jdx.cloudfront.net/bluebar/b4d4e0e/images/shared/noise-1.png), -moz-linear-gradient(top, #517fa4, #306088);
  background-image: url(//d36xtkk24g8jdx.cloudfront.net/bluebar/b4d4e0e/images/shared/noise-1.png), -o-linear-gradient(top, #517fa4, #306088);
  background-image: url(//d36xtkk24g8jdx.cloudfront.net/bluebar/b4d4e0e/images/shared/noise-1.png), -ms-linear-gradient(top, #517fa4, #306088);
  background-image: url(//d36xtkk24g8jdx.cloudfront.net/bluebar/b4d4e0e/images/shared/noise-1.png), linear-gradient(top, #517fa4, #306088);
  background-position: 50% 50%;
  margin: -1px -1px 1px; }

.igDialogTitle {
  margin: 0;
  line-height: 44px; }

.formAdjacent label {
  display: inline-block;
  width: 35%; }

.igDialogClose {
  position: fixed;
  right: 8px;
  top: 0;
  display: block;
  font: 30px/30px "Helvetica Neue", Arial, Helvetica, sans-serif;
  font-weight: bold;
  font-style: normal;
  color: #fff;
  cursor: pointer; }

.igDialogTitle {
  color: #fff;
  font-size: 18px;
  text-align: center;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.4); }

@media screen and (max-width: 920px) {
  .igDialogLayer {
    padding: 32px; } }
@media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min--moz-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2 / 1), only screen and (min-device-pixel-ratio: 2), only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx) {
  .igDialogClose,
  .igdContentWrap {
    -webkit-transform: translate3d(0, 0, 0);
    -moz-transform: translate3d(0, 0, 0);
    -o-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0); } }
</style>...

2 个答案:

答案 0 :(得分:1)

以下适用于我,我确实在该页面上看到<style>标记。

我认为您应该实施WebBrowser功能控制(具体而言,FEATURE_BROWSER_EMULATION,如下所示SetFeatureBrowserEmulation):

namespace WindowsFormApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            SetFeatureBrowserEmulation();
            InitializeComponent();
            this.webBrowser1.Navigate("http://instagram.com/p/lhG0xdE8B4/#");
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            this.textBox1.Text = this.webBrowser1.Document.GetElementsByTagName("head")[0].OuterHtml;
        }

        static void SetFeatureBrowserEmulation()
        {
            if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
                return;
            var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
            Microsoft.Win32.Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
                appName, 9000, Microsoft.Win32.RegistryValueKind.DWord);
        }
    }
}

答案 1 :(得分:0)

更改

Microsoft.Win32.Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
                appName, 9000, Microsoft.Win32.RegistryValueKind.DWord);

Microsoft.Win32.Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
                appName, 11000, Microsoft.Win32.RegistryValueKind.DWord);

实际上解决了问题(开始获取样式标签)。可能与我运行IE 11有关。