Pergunta

Here's my problem: I am using the WebBrowser to load a page, to be more precise this page. As you can see, the page has a lot of js scripts that load dynamically styles into the page (please notice all the style tags). In my application, I see the page as it is (with styles loaded correctly), but when I try to export the HMTL:

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

I do get the HTML of the page, but the style tags are empty (which is a problem). How can I get the HTML with the styles?

EDIT: I forgot to mention that I am using the windows forms browser BUT it is in a windows forms host in WPF. Short example would be:

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);
        }

What I am getting:

...<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>...

What I should be getting:

...<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>...
Foi útil?

Solução 2

Changing

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

to

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

actually solved the problem (started getting the style tags). Might have something to do with me running IE 11.

Outras dicas

The following works for me, I do see the <style> tags on that page.

I think you should implement the WebBrowser Feature Control (specifically, FEATURE_BROWSER_EMULATION, as SetFeatureBrowserEmulation does below):

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);
        }
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top