注意,要添加两个COM引用
Microsoft HTML Object Library
Microsoft Internet Controls
BUTTON1打开百度查询
BUTTON2模拟输入用户密码,并登录。
private void button1_Click(object sender, EventArgs e) { SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer(); ie.DocumentComplete += ie_DocomentComplete; ie.Navigate("http://www.baidu.com"); ie.Visible = true; compWait(); mshtml.HTMLDocument doc = ie.Document; doc.getElementById("kw").innerText = "中国"; doc.getElementById("su").click(); } private bool ie_read = false; private void ie_DocomentComplete(object pDisp, ref object URL) //加载完成事件 { ie_read = true; } private void compWait() //等待,直到加载完成 { while (ie_read != true) { Application.DoEvents(); } } private void button2_Click(object sender, EventArgs e) { SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer(); ie.DocumentComplete += ie_DocomentComplete; ie.Navigate("http://10.100.200.300/XXXXX/XXXXX.do"); ie.Visible = true; compWait(); mshtml.HTMLDocument doc = ie.Document; mshtml.IHTMLElementCollection userids = doc.getElementsByName("userid"); foreach (mshtml.IHTMLElement userid in userids) { userid.innerText = "xiaoming"; } mshtml.IHTMLElementCollection passwords = doc.getElementsByName("password"); foreach (mshtml.IHTMLElement password in passwords) { password.innerText = "888888"; } mshtml.IHTMLElementCollection syaincds = doc.getElementsByName("syaincd"); foreach (mshtml.IHTMLElement syaincd in syaincds) { syaincd.innerText = "0"; } mshtml.IHTMLElementCollection logins = doc.getElementsByName("login"); foreach (mshtml.IHTMLElement login in logins) { login.click(); } System.Threading.Thread.Sleep(3000); mshtml.IHTMLElementCollection tencds = doc.getElementsByName("login"); foreach (mshtml.IHTMLElement tencd in tencds) { tencd.click(); } }