急,如何获取HtmlElement.InvokeMember之后的HTML数据

kensouterry 2009-12-28 02:47:08
一个网页程序(界面上有1个TextBox和1个Button)
主体代码:

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方法

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.DocumentText;

Console.WriteLine(wb.DocumentText);
}


[STAThread]
//[PermissionSet(SecurityAction.Demand, Name = "FullTrust ")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
static void Main(string[] args)
{
Program p1 = new Program();
p1.ExecuteSearch();


Console.ReadLine();
}

}
}


问题在TestObtainElement()函数中,调用了Button1控件的Click事件后,从WebBrowser中得到的DocumentText数据与调用之前的WebBrowser.DocumentText一样,我想应该是调用Click事件出了错。
希望各位兄弟姐妹能帮我解决一下!
...全文
485 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
joeguonet 2011-10-13
  • 打赏
  • 举报
回复
顶一下 我也遇到同样的问题了
kensouterry 2009-12-28
  • 打赏
  • 举报
回复
有没有人帮我解决一下啊?这个问题很复杂吗?
kensouterry 2009-12-28
  • 打赏
  • 举报
回复
晕,我还以为得到答案了,继续在线等!
vip__888 2009-12-28
  • 打赏
  • 举报
回复
没搞过 用空搞下
zenowolf 2009-12-28
  • 打赏
  • 举报
回复
额,不熟,帮顶
zenowolf 2009-12-28
  • 打赏
  • 举报
回复
路过,围观ing。
kensouterry 2009-12-28
  • 打赏
  • 举报
回复
还是来人帮我顶一下好吗?顶了我也送分!

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧