C# HtmlDocument полный HTML

Вот моя проблема: я использую WebBrowser загрузить страницу, а точнее эту страницу. Как видите, на странице есть много js-скриптов, которые динамически загружают стили на страницу (обратите внимание на все теги стилей). В моем приложении я вижу страницу как есть (с правильно загруженными стилями), но когда я пытаюсь экспортировать HMTL:

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

Я получаю HTML-код страницы, но теги стиля пусты (что является проблемой). Как я могу получить HTML со стилями?

РЕДАКТИРОВАТЬ: я забыл упомянуть, что я использую браузер Windows Forms, но он находится в хосте Windows Forms в WPF. Коротким примером будет:

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 ответа

Решение

Изменения

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

Следующие работы для меня, я вижу <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);
        }
    }
}
Другие вопросы по тегам