日期:2014-05-18  浏览次数:21150 次

急,如何获取HtmlElement.InvokeMember之后的HTML数据
一个网页程序(界面上有1个TextBox和1个Button)
主体代码:
C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TextBox1.Text="Hello,World";
        Button1.Click += new EventHandler(Button1_Click);
    }

    void Button1_Click(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        this.TextBox1.Text = "Hello World,Change!";
    }
}




现在我要在一个控制台程序里面调用此Web程序中Button控件的Click方法
C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;
using System.Security.Permissions;

namespace 模拟点击操作_Console
{
    class Program
    {
        WebBrowser wb;
        //返回网页内容字符串
        StringBuilder allInfo = new StringBuilder();
        //Html 文档对象
        HtmlDocument doc;

        /// <summary>
        /// 截取某网页中的所有数据--测试函数
        /// </summary>
        /// <returns></returns>
        public string TestObtainWebpageContent()
        {
            string address = @"http://localhost:1106/TestSite";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //response.ContentType = "text/xml";

            char[] buf = new char[256];
            StreamReader resStream = new StreamReader(response.GetResponseStream(),Encoding.GetEncoding("big5"));
            
            int count = resStream.Read(buf, 0, buf.Length);
            while (count > 0)
            {
                // Dumps the 256 characters on a string and displays the string to the console.
                String str = new String(buf, 0, count);
                allInfo.Append(str);
                count = resStream.Read(buf, 0, count);
            }
            resStream.Close();
            return allInfo.ToString();
        }

        /// <summary>
        /// 主调函数
        /// </summary>
        private void ExecuteSearch()
        {
            wb = new WebBrowser();
            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
            wb.ScriptErrorsSuppressed = true;
            //导航到指定的URL地址中HTML源码---??此时给定某URL
            TestObtainWebpageContent();
            wb.Navigate("D:\\12-28\\模拟点击操作_Console\\test.html");
            //初始化HtmlDocument对象
            doc=wb.Document.OpenNew(true);
            doc.Write(allInfo.ToString());
            wb_DocumentCompleted(null, null);

            wb.Dispose();
        }

        /// <summary>
        /// 文档加载完成事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            TestObtainElement();
        }

        /// <summary>
        /// 读取下一页的内容并进行显示--测试函数
        /// </summary>
        private void TestObtainElement()
        {
            string s = wb.DocumentText;

            HtmlElement elem = wb.Document.Body.All["Button1"];
            elem.InvokeMember("Click");
            //IHTMLElement element = (IHTMLElement)input.DomElement;
            //element.Click();
            string s1 = wb.DocumentT