Microsfot 及 其他一些廠商的指紋機有提供一個 網頁自動登入的功能
如 http://www.fineart.com.tw/fineart_web/products/fp/fp_wl.htm這是如何辦到的呢? 下面範例示範如何自動找到正確的IE視窗並自動登入Gmail or Yahoo Mail
使用這個範例必需在參考中加入 (在COM的那一個頁籤中)
Microsoft HTML Object Library 及 Microsoft Internet Controls
1private void AutoSignInGmail()
2{
3string ID = "YourID";
4string PWD = "YourPassword";
5
6mshtml.IHTMLDocument2 MyDoc; //網頁內文
7mshtml.HTMLInputElementClass MyInput; //Input Tag
8SHDocVw.InternetExplorer AutoIE = null; //IE視窗
9object missing = null; //開起IE navigate用
10
11#region 找看看有沒有現在的IE是在登入頁
12//取得目前 Shell 的所有 Windows
13SHDocVw.ShellWindowsClass shellWindows = new SHDocVw.ShellWindowsClass();
14//對所有的視窗
15foreach (SHDocVw.InternetExplorer MyIE in shellWindows)
16{
17//如果是IE
18if (MyIE.FullName.ToUpper().IndexOf("IEXPLORE.EXE") > 0)
19{
20MyDoc = (mshtml.IHTMLDocument2)MyIE.Document;
21//我們以Title判斷登入頁是否存在
22if (MyDoc.title == "歡迎使用 Gmail") //Gmail
23//if (MyDoc.title == "Yahoo!奇摩電子信箱") //YahooMail
24{
25AutoIE = MyIE; //如果有找到
26break;
27}
28}
29}
30shellWindows = null;
31#endregion
32
33#region 如果沒找到開一個新IE
34//如果沒找到
35if (AutoIE == null)
36{
37//建一個新IE
38AutoIE = new SHDocVw.InternetExplorer();
39AutoIE.Visible = true;
40AutoIE.Navigate("http://gmail.com/",
41ref missing, ref missing, ref missing, ref missing); //Gmail
42//AutoIE.Navigate("http://mail.kimo.com.tw/",
43// ref missing, ref missing, ref missing, ref missing); //YahooMail
44
45//等待頁面載入完成, 也可以用DocumentComplete Event
46while (AutoIE.StatusText.IndexOf("完成")==-1)
47Application.DoEvents();
48}
49#endregion
50
51#region 搜尋IE網頁內文找到正確欄位並填入值
52//取得IE網頁內文
53MyDoc = (mshtml.IHTMLDocument2)AutoIE.Document ;
54//對每一個Tag
55foreach (mshtml.IHTMLElement MyElement in MyDoc.all)
56{
57//如果是 Input Tag
58if (MyElement.tagName == "INPUT")
59{
60//轉型
61MyInput = ((mshtml.HTMLInputElementClass)MyElement);
62
63//是不是帳號欄位
64if (MyInput.name=="Email") //Gmail
65//if (MyInput.name=="login") //YahooMail
66MyInput.value = ID;
67![]()
68//是不是密碼欄位
69if (MyInput.name=="Passwd") //Gmail
70//if (MyInput.name=="passwd") //YahooMail
71MyInput.value = PWD;
72![]()
73//是不是登入的按鈕
74if (MyInput.name=="null") //Gmail
75//if (MyInput.name=="submit") //YahooMail
76MyInput.click();
77}
78}
79#endregion
80
81AutoIE = null;
82}
83
轉至http://blog.blueshop.com.tw/timothychi/archive/2006/03/09/19083.aspx
2010年7月30日 星期五
如何自動找到正確的IE視窗並自動登入Gmail or Yahoo Mail
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言