C# webBrowser 脚本错误

C# webBrowser script error

我在尝试使用 webBrowser.Navigate("https://home.nest.com/") 加载页面时不断收到脚本错误。它可以在我的普通互联网浏览器中正常运行,但在我的程序中却不行。

谁能给我指出正确的方向?

脚本错误一直发生在集成的 Internet Explorer WebBrowser 控件中,即使它使用的是版本 11。现代网站严重依赖大量 Javascript 文件和动态呈现。您可以通过在常规浏览器中观察该页面加载来看到这一点。只是有时候控件不能切。

您可能想尝试一些替代的浏览器控件。不能保证它会与它们中的任何一个一起工作,但至少可以尝试一下。

  • Awesomium :最初基于 Chromium。我不知道他们是否仍然整合了 Chromium 的变化,或者他们是否已经朝着自己的方向发展。它免费供个人使用,也可用于商业用途,收入低于 10 万美元。
  • DotNetBrowser:将基于 Chromium 的 WPF/WinForms 组件嵌入到您的 .NET 应用程序中,以显示使用 HTML5、CSS3、JavaScript 构建的现代网页, Silverlight 等
  • geckofx : 用于在 .NET 应用程序中嵌入 Mozilla Gecko (Firefox) 的开源组件。
  • Xilium.CefGlue:Marshall A. Greenblatt 的 Chromium 嵌入式框架 (CEF) 的 .NET/Mono 绑定。
  • BrowseEmAll:BrowseEmAll.Cef (Chrome), BrowseEmAll.Gecko (Firefox), BrowseEmAll Core API(Chrome,Firefox,IE - 商业版)

可能还有其他的,但如果您想走这条路,这应该会让您从一些更受欢迎的活跃项目开始。

您应该将程序名称添加到寄存器中 HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION 与普通互联网浏览器一样使用最新功能。

对我来说,值8000 (0x1F40) - IE8模式可以解决很多脚本错误问题。

参考:

Use latest version of Internet Explorer in the webbrowser control

作为this link答案:

您只能添加这一行:

webBrowser.ScriptErrorsSuppressed = true;

WebBrowser 控件能够呈现大多数网页,但默认情况下它会尝试以兼容模式呈现页面(几乎是 IE7,因此存在问题)。如果您正在构建自己的页面,这很简单,只需将以下标记添加到页眉,它应该可以正常显示...

<meta http-equiv="X-UA-Compatible" content="IE=Edge" />

但是,如果您尝试呈现无法添加标签的第三方网站,事情就会变得更加困难。如上所述,如果它只是在您自己的计算机上,您可以使用注册表项 (HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION)。

如果这些选项都不是可行的解决方案,那么使用不同的浏览器控件(再次强调,上面的好建议)几乎是您唯一的选择。

https://docs.microsoft.com/en-gb/archive/blogs/patricka/controlling-webbrowser-control-compatibility

上有一篇关于控制浏览器控件兼容模式的很棒的博客
  private void Form1_Load(object sender, EventArgs e)
  {
            var appName = Process.GetCurrentProcess().ProcessName + ".exe";
            SetIE8KeyforWebBrowserControl(appName);

            webBrowser1.ScriptErrorsSuppressed = true;
  }



private void SetIE8KeyforWebBrowserControl(string appName)
{
     RegistryKey Regkey = null;
     try
     {
         // For 64 bit machine
         if (Environment.Is64BitOperatingSystem)
              Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
         else  //For 32 bit machine
               Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);

          // If the path is not correct or
          // if the user haven't priviledges to access the registry
          if (Regkey == null)
          {
              MessageBox.Show("Application Settings Failed - Address Not found");
              return;
          }

          string FindAppkey = Convert.ToString(Regkey.GetValue(appName));

          // Check if key is already present
          if (FindAppkey == "8000")
          {
              MessageBox.Show("Required Application Settings Present");
              Regkey.Close();
              return;
          }

          // If a key is not present add the key, Key value 8000 (decimal)
          if (string.IsNullOrEmpty(FindAppkey))
              Regkey.SetValue(appName, unchecked((int)0x1F40), RegistryValueKind.DWord);

           // Check for the key after adding
           FindAppkey = Convert.ToString(Regkey.GetValue(appName));

           if (FindAppkey == "8000")
               MessageBox.Show("Application Settings Applied Successfully");
           else
               MessageBox.Show("Application Settings Failed, Ref: " + FindAppkey);
       }
       catch (Exception ex)
       {
           MessageBox.Show("Application Settings Failed");
           MessageBox.Show(ex.Message);
       }
       finally
       {
           // Close the Registry
           if (Regkey != null)
               Regkey.Close();
       }
   }

您甚至可以将注册表值设置为 11000 以获得最新版本的 IE!!