操纵IE,模拟用户登录

操作IE浏览器,模拟用户登录,首先需要两个程序集:shdocvw.dll和mshtml.dll;

新建项目将程序集引用到项目中;

代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using mshtml;
  7 using SHDocVw;
  8 using System.Threading;
  9 using System.Diagnostics;
 10 
 11 namespace StudyPrograms
 12 {
 13     class Program
 14     {
 15         static AutoResetEvent documentComplete = new AutoResetEvent(false);
 16  
 17         static void Main(string[] args)
 18         {
 19             Console.WriteLine(DateTime.Now.ToString() + " 开始");
 20             InternetExplorer ie = GetInternetExplorer();
 21             if (ie != null)
 22             {
 23                 Run(ie);
 24             }
 25             Console.WriteLine(DateTime.Now.ToString() + " 结束");
 26         }
 27 
 28         private static InternetExplorer GetInternetExplorer()
 29         {
 30             InternetExplorer ie = null;
 31             Console.WriteLine(DateTime.Now.ToString() + " 加载IE实例");
 32 
 33             //查找是否有打开的IE进程窗口,如果已经包含CSDN登录页,则得到此进程
 34             Process[] processes = Process.GetProcesses();
 35             Process process = null;
 36             foreach (Process p in processes)
 37             {
 38                 if (string.Compare(p.ProcessName, "iexplore", true) == 0)
 39                 {
 40                     if (p.MainWindowTitle.IndexOf("CSDN 用户登录") >= 0) // CSDN 用户登录 - Windows Internet Explorer
 41 
 42                     {
 43                         process = p;
 44                         break;
 45                     }
 46                 }
 47             }
 48 
 49             //如果没有则启动IE实例
 50             if (process == null)
 51             {
 52                 process = Process.Start("iexplore.exe", "about:blank");
 53             }
 54 
 55             if (process == null)
 56             {
 57                 Console.WriteLine(DateTime.Now.ToString() + " 无法启动IE");
 58                 return null;
 59             }
 60 
 61             Thread.Sleep(3000);
 62             try
 63             {
 64 
 65                 Console.WriteLine(DateTime.Now.ToString() + " Process Handle: " + process.MainWindowHandle.ToString());
 66             }
 67             catch (Exception ex)
 68             {
 69                 Console.WriteLine(ex.Message);
 70                 return null;
 71             }
 72 
 73             ShellWindows browsers = new ShellWindows();
 74             Console.WriteLine(DateTime.Now.ToString() + " 活动浏览器数量: " + browsers.Count);
 75             if (browsers.Count == 0)
 76             {
 77                 Console.WriteLine(DateTime.Now.ToString() + " 未找到IE");
 78             }
 79 
 80             //如果找到匹配的IE进程,则把当前InternetExplorer对象连接到正在运行的IE程序
 81             Console.WriteLine(DateTime.Now.ToString() + " 附加到IE");
 82             int i = 0;
 83             while (i < browsers.Count && ie == null)
 84             {
 85                 InternetExplorer e = browsers.Item(i) as InternetExplorer;
 86                 if (e != null)
 87                 {
 88                     if (e.HWND == (int)process.MainWindowHandle)
 89                     {
 90                         ie = e;
 91                         break;
 92                     }
 93                 }
 94                 ++i;
 95             }
 96             if (ie == null)
 97             {
 98                 Console.WriteLine(DateTime.Now.ToString() + " 附加到IE失败");
 99             }
100             return ie;
101         }
102 
103         private static void Run(InternetExplorer ie)
104         {
105             ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
106 
107             try
108             {
109                 HTMLDocument document = null;
110  
111                 object o = new object();
112                 //导航到 博客园 登录页
113                 ie.Navigate("http://passport.cnblogs.com/user/signin?ReturnUrl=http%3A%2F%2Fwww.cnblogs.com%2Fcate%2Fbeginner%2F", ref o, ref o, ref o, ref o);
114                 documentComplete.WaitOne(1000, true);
115 
116                 document = ie.Document as HTMLDocument;
117                 if (document != null)
118                 {
119                     //以下操纵IE Shell
120                     HTMLInputElement loginname = document.getElementById("input1") as HTMLInputElement;
121                     if (loginname != null)
122                     {
123                         loginname.value = "用户名";
124                     }
125                     HTMLInputElement pwd = document.getElementById("input2") as HTMLInputElement;
126                     if (pwd != null)
127                     {
128                         pwd.value = "密码";
129                     }
130                     //HTMLInputButtonElement imagecode = document.getElementById("ctl00_CPH_Content_tb_ExPwd") as HTMLInputButtonElement;
131                     //if (imagecode != null)
132                     //{
133                     //    Console.Write("输入验证码:");
134                     //    //控制台窗口输入验证码
135                     //    string code = Console.ReadLine();
136                     //    imagecode.value = code;
137                     //}
138                     HTMLButtonElement signin = document.getElementById("signin") as HTMLButtonElement;
139                     if (signin != null)
140                     {
141                         signin.click();
142                         documentComplete.WaitOne(1000, true);
143                     }
144                 }
145             }
146             catch (Exception ex)
147             {
148                 Console.WriteLine(ex.Message);
149             }
150             
151         //    ie.Quit();
152         }
153 
154         private static void ie_DocumentComplete(object pDisp, ref object URL)
155         {
156             documentComplete.Set();
157         }
158     }
159 }

使用前关闭所有IE浏览器,然后运行,否则会出现异常,目前没解决

原文地址:https://www.cnblogs.com/yanjc/p/4626236.html