急,如何获取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.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事件出了错。
希望各位兄弟姐妹能帮我解决一下!
[解决办法]
路过,围观ing。
[解决办法]
没搞过 用空搞下