16

我正在尝试通过 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 为“未初始化”,WebBrowser.Document = null,它总体上似乎完全不受我的调用的影响。

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

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

4

4 回答 4

19

您应该处理 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");
    }
  }
}
于 2010-04-30T20:22:34.170 回答
3

您可能需要将控件托管在父窗口中。您可以通过将浏览器控件移出屏幕而不显示承载浏览器控件的窗口来执行此操作而不会违反要求。对于开发来说,“看到”它确实加载了一些用于测试、验证等的东西也可能很有用。

所以试试:

// 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)

于 2010-04-30T20:23:34.537 回答
2

我遇到了这个问题,我没有意识到我已经卸载了 Internet Explorer。如果你有,什么都不会发生,因为 WebBrowser 控件只实例化 IE。

于 2012-11-13T16:53:57.927 回答
0

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

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

于 2010-04-30T20:27:53.193 回答