.NET C#:WebBrowser控件Navigate()不加载目标URL

我正在尝试以编程方式通过WebBrowser控件加载网页,目的是测试页面及其JavaScript函数。 基本上,我想比较通过此控件运行的HTML和JavaScript与已知输出,以确定是否存在问题。

但是,我在创建和导航WebBrowser控件时遇到了麻烦。 下面的代码旨在将HtmlDocument加载到WebBrowser.Document属性中:

WebBrowser wb = new WebBrowser(); wb.AllowNavigation = true; wb.Navigate("http://www.google.com/"); 

在Navigate()运行后通过Intellisense检查Web浏览器的状态时,WebBrowser.ReadyState为’Uninitialized’,WebBrowser.Document = null,并且它总体上看起来完全不受我的调用的影响。

在上下文中,我在Windows窗体对象之外运行此控件:我不需要加载窗口或实际查看页面。 要求规定需要简单地执行页面的JavaScript并检查生成的HTML。

非常感谢任何建议,谢谢!

您应该处理WebBrowser.DocumentComplete事件,一旦引发该事件,您将拥有Document等。

 wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted); 

 private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { WebBrowser wb = sender as WebBrowser; // wb.Document is not null at this point } 

这是一个完整的示例,我在Windows窗体应用程序中快速完成并进行了测试。

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { WebBrowser wb = new WebBrowser(); wb.AllowNavigation = true; wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted); wb.Navigate("http://www.google.com"); } private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { WebBrowser wb = sender as WebBrowser; // wb.Document is not null at this point } } 

编辑:这是一个从控制台应用程序运行窗口的简单代码版本。 您当然可以进一步将事件暴露给控制台代码等。

 using System; using System.Windows; using System.Windows.Forms; namespace ConsoleApplication1 { class Program { [STAThread] static void Main(string[] args) { Application.Run(new BrowserWindow()); Console.ReadKey(); } } class BrowserWindow : Form { public BrowserWindow() { ShowInTaskbar = false; WindowState = FormWindowState.Minimized; Load += new EventHandler(Window_Load); } void Window_Load(object sender, EventArgs e) { WebBrowser wb = new WebBrowser(); wb.AllowNavigation = true; wb.DocumentCompleted += wb_DocumentCompleted; wb.Navigate("http://www.bing.com"); } void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { Console.WriteLine("We have Bing"); } } } 

您可能需要在父窗口中托管控件。 您可以在不破坏要求的情况下执行此操作,只需通过将其移出屏幕即可显示承载浏览器控件的窗口。 对于开发来说,“看到”它确实会加载用于测试,validation等的东西也可能是有用的。

所以尝试:

 // in a form's Load handler: WebBrowser wb = new WebBrowser(); this.Controls.Add(wb); wb.AllowNavigation = true; wb.Navigate("http://www.google.com/"); 

还要检查在通过IDE实例化WebBrowser对象时在WebBrowser对象上设置的其他属性。 例如,创建一个表单,将浏览器控件放到它上面,然后检查表单的设计器文件以查看生成的代码。 您可能缺少一些需要设置的关键属性。 我以这种方式在我的代码中发现了许多遗漏,并且还学会了如何以编程方式正确地实例化可视对象。

PS如果您使用主机窗口,它应该只在开发期间可见。 你会以某种方式隐藏生产。

另一种方法:

你可以通过尝试这样的东西来“原始”:

  System.Net.WebClient wc = new System.Net.WebClient(); System.IO.StreamReader webReader = new System.IO.StreamReader( wc.OpenRead("http://your_website.com")); string webPageData = webReader.ReadToEnd(); 

…然后RegEx或解析webPageData以满足您的需求。 或者您是否需要页面中的jscript才能实际执行? (.NET 4.0应该可以实现)

我有这个问题,我没有意识到我已经卸载了Internet Explorer。 如果你有,没有任何事情会发生,因为WebBrowser控件只实例化IE。

Webbrowser控件只是Internet Explorer的包装器。

您可以设置一个不可见的Windows窗体窗口来完全实例化它。