.NET C#WebBrowser控件导航()不会加载有针对性的网址 [英] .NET C#: WebBrowser control Navigate() does not load targeted URL

查看:157
本文介绍了.NET C#WebBrowser控件导航()不会加载有针对性的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过web浏览器控制测试页面和放大器的意图编程方式加载一个网页;它的JavaScript函数。基本上,我想比较HTML和放大器; JavaScript的通过对已知的输出这种控制运行,以确定是否有问题。

I'm trying to programmatically load a web page via the WebBrowser control with the intent of testing the page & it's JavaScript functions. Basically, I want to compare the HTML & JavaScript run through this control against a known output to ascertain whether there is a problem.

不过,我无法简单地创建和浏览WebBrowser控件。在code以下旨在加载的HTMLDocument到WebBrowser.Document属性:

However, I'm having trouble simply creating and navigating the WebBrowser control. The code below is intended to load the HtmlDocument into the WebBrowser.Document property:

WebBrowser wb = new WebBrowser();
wb.AllowNavigation = true;

wb.Navigate("http://www.google.com/");

在通过审查后,智能感知导航()运行Web浏览器的状态下,WebBrowser.ReadyState是未初始化,WebBrowser.Document = NULL,并通过我的呼吁整体出现完全不受影响。

When examining the web browser's state via Intellisense after Navigate() runs, the WebBrowser.ReadyState is 'Uninitialized', WebBrowser.Document = null, and it overall appears completely unaffected by my call.

在上下文笔记,我运行Windows窗体对象之外的这种控制:我并不需要加载一个窗口或实际看网页。需求决定了需要简单地执行该页面的JavaScript和检查结果HTML。

On a contextual note, I'm running this control outside of a Windows form object: I do not need to load a window or actually look at the page. Requirements dictate the need to simply execute the page's JavaScript and examine the resultant HTML.

任何建议都大大AP preciated,谢谢!

Any suggestions are greatly appreciated, thanks!

推荐答案

您应该处理WebBrowser.DocumentComplete事件,一旦事件引发你会有文件等。

You should handle the WebBrowser.DocumentComplete event, once that event is raised you will have the Document etc.

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赶紧做了窗体应用程序和测试。

Here is a complete example, that I quickly did in a Windows Forms application and tested.

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

编辑:这是一个简单的版本,code运行从控制台应用程序的窗口。当然,你可以更进一步地揭露事件控制台code等。

Here is a simple version of code that runs a window from a console application. You can of course go further and expose the events to the console code etc.

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

这篇关于.NET C#WebBrowser控件导航()不会加载有针对性的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆