using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using SHDocVw;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ShellWindows IEList = new ShellWindowsClass();
try
{
foreach (IWebBrowser browser in IEList)
{
object doc = browser.Document;
object value = doc.GetType().GetProperty("Title").GetValue(doc, null);
if (doc.ToString() == "TestPage")
{
object body = doc.GetType().GetProperty("Body").GetValue(doc, null);
object all = body.GetType().GetProperty("All").GetValue(body, null);
object LoginName = all.GetType().GetProperty("this").GetValue(all, new object[] { "LoginName" });
LoginName.GetType().GetProperty("Value").SetValue(LoginName, "eddie", null);
object LoginPassword = all.GetType().GetProperty("this").GetValue(all, new object[] { "LoginPassword" });
LoginPassword.GetType().GetProperty("Value").SetValue(LoginPassword, "123456", null);
object clickme = all.GetType().GetProperty("this").GetValue(all, new object[] { "clickme" });
clickme.GetType().GetMethod("Click").Invoke(clickme, null);
}
}
}
catch (Exception)
{
}
Console.WriteLine("Press any key to end applicatino...");
Console.ReadKey();
}
}
}引用Microsoft internet controls
Document 返回的是Object,用GetType看了下,實際的值是__ComObject
用反射賦值,但是__ComObject貌似和Document的方法,屬性對不上號,等待高手解釋
這樣也報錯,暫時無辦法
foreach(SHDocVw.InternetExplorer cIE in IElists)
{
HtmlElementCollection hrefList = ((HtmlDocument)cIE.Document).GetElementsByTagName("a");
}运行报错:无法将类型为“mshtml.HTMLDocumentClass”的 COM 对象强制转换为类类型“System.Windows.Forms.HtmlDocument”。表示 COM 组件的类型实例不能强制转换为不表示 COM 组件的类型;不过,只要基础 COM 组件支持对接口 IID 的 QueryInterface 调用,就能将这些实例强制转换为接口。
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using SHDocVw;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.IO;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
HtmlDocument d;
ShellWindows IEList = new ShellWindowsClass();
try
{
foreach (IWebBrowser browser in IEList)
{
object doc = browser.Document;
if (doc.GetType().ToString() == "mshtml.HTMLDocumentClass")
{
object value = doc.GetType().GetProperty("title").GetValue(doc, null);
if (value.ToString() == "TestPage")
{
MethodInfo method = doc.GetType().GetMethod("getElementById");
object LoginName = method.Invoke(doc, new object[] { "LoginName" });
LoginName.GetType().GetProperty("value").SetValue(LoginName, "eddie", null);
object LoginPassword = method.Invoke(doc, new object[] { "LoginPassword" });
LoginPassword.GetType().GetProperty("value").SetValue(LoginPassword, "123456", null);
object clickme = method.Invoke(doc, new object[] { "clickme" });
clickme.GetType().GetMethod("click").Invoke(clickme, null);
}
}
}
}
catch (Exception)
{
}
Console.WriteLine("Press any key to end applicatino...");
Console.ReadKey();
}
}
}HTML代碼如下:<html>
<head>
<title>TestPage</title>
</head>
<body>
<input type="text" id="LoginName"/><br/>
<input type="password" id="LoginPassword"/><br/>
<input type="submit" id="clickme"/>
</body>
<html>
Microsoft.mshtml.dll,microsoft Html object libary.dll
之后你就可以通过如下方法来实现:
这是我实现的部分代码,你可以参考一下:
private void Form1_Load(object sender, System.EventArgs e)
{
if (exitflag==true)
{
Application.Exit();
}
else
{
this.txt_pwd.Text=m_strupwd;
this.axWebBrowser1.Navigate(this.m_strCardUserLogin,ref nullObject,ref nullObject, ref nullObject,ref nullObject);
this.label3.Text="正在连接请稍侯…………";
this.button4.Enabled=false;
}
/*newUrl="http://bizweb.sdeport.com"+response.GetResponseHeader("Location");
int start = newUrl.IndexOf("(");
int end =newUrl.IndexOf(")");
//得到每次传递网址的一个编号
Sid=newUrl.Substring(start,end-start+1);*/
}
private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
this.LoginPage();
}
/// <summary>
/// 登陆内容填写/执行登陆事件
/// </summary>
/// <returns></returns>
public void LoginPage()
{
try
{
mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)axWebBrowser1.Document;
mshtml.HTMLInputElementClass textPassword = (mshtml.HTMLInputElementClass)doc.getElementById("textPassword");
textPassword.value=this.m_strupwd;
mshtml.HTMLInputElementClass btnLogin = (mshtml.HTMLInputElementClass)doc.getElementById("btnLogin");
btnLogin.click();
//flag=true;
}
catch(Exception ex)
{
//flag=false;
MsgBox("错误代码:407"+"\r\n"+ex.Message);
exitflag=true;
}
if (exitflag==true)
{
shutdown();
}
}
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Net;
这是需要引用的名称空间
原帖:http://topic.csdn.net/t/20061027/09/5112780.html