操纵IE浏览器模拟用户登录CSDN

大多数C#程序员对于使用HttpRequest、HttpResponse、WebClient这样的类向Web服务器发出请求并取得响应结果并不陌生。

但有时我们想模拟用户操纵浏览器的场景(尤其是在自动化测试时),那么我们可以选择使用mshtml.dll和shdocvw.dll来完成对IE DOM的操作以实现此功能。

假设我们想访问http://community.csdn.net/,并在这之前已经登录,那么实际上我们应该访问http://passport.csdn.net/UserLogin.aspx?from=http://community.csdn.net/,并在此页面输入用户名、密码、验证码并点击登录按钮。

首先我们新建一个Console项目,并添加对mshtml.dll(添加引用--.NET选项卡--Microsoft.mshtml)和shdocvw.dll(添加引用--COM选项卡--Microsoft Internet Controls)的引用。

 

详细内容请参考代码注释

 

[c-sharp]  view plain copy
  1. using System;  
  2. using System.Diagnostics;  
  3. using System.Threading;  
  4. using mshtml;  
  5. using SHDocVw;  
  6.   
  7. namespace IEDemo  
  8. {  
  9.     class Program  
  10.     {  
  11.         static AutoResetEvent documentComplete = new AutoResetEvent(false);  
  12.    
  13.         static void Main(string[] args)  
  14.         {  
  15.             Console.WriteLine(DateTime.Now.ToString() + " 开始");  
  16.             InternetExplorer ie = GetInternetExplorer();  
  17.             if (ie != null)  
  18.             {  
  19.                 Run(ie);  
  20.             }  
  21.             Console.WriteLine(DateTime.Now.ToString() + " 结束");  
  22.         }  
  23.   
  24.         private static InternetExplorer GetInternetExplorer()  
  25.         {  
  26.             InternetExplorer ie = null;  
  27.             Console.WriteLine(DateTime.Now.ToString() + " 加载IE实例");  
  28.   
  29.             //查找是否有打开的IE进程窗口,如果已经包含CSDN登录页,则得到此进程  
  30.             Process[] processes = Process.GetProcesses();  
  31.             Process process = null;  
  32.             foreach (Process p in processes)  
  33.             {  
  34.                 if (string.Compare(p.ProcessName, "iexplore"true) == 0)  
  35.                 {  
  36.                     if (p.MainWindowTitle.IndexOf("CSDN 用户登录") >= 0) // CSDN 用户登录 - Windows Internet Explorer  
  37.   
  38.                     {  
  39.                         process = p;  
  40.                         break;  
  41.                     }  
  42.                 }  
  43.             }  
  44.   
  45.             //如果没有则启动IE实例  
  46.             if (process == null)  
  47.             {  
  48.                 process = Process.Start("iexplore.exe""about:blank");  
  49.             }  
  50.   
  51.             if (process == null)  
  52.             {  
  53.                 Console.WriteLine(DateTime.Now.ToString() + " 无法启动IE");  
  54.                 return null;  
  55.             }  
  56.   
  57.             Thread.Sleep(3000);  
  58.             try  
  59.             {  
  60.   
  61.                 Console.WriteLine(DateTime.Now.ToString() + " Process Handle: " + process.MainWindowHandle.ToString());  
  62.             }  
  63.             catch (Exception ex)  
  64.             {  
  65.                 Console.WriteLine(ex.Message);  
  66.                 return null;  
  67.             }  
  68.   
  69.             ShellWindows browsers = new ShellWindows();  
  70.             Console.WriteLine(DateTime.Now.ToString() + " 活动浏览器数量: " + browsers.Count);  
  71.             if (browsers.Count == 0)  
  72.             {  
  73.                 Console.WriteLine(DateTime.Now.ToString() + " 未找到IE");  
  74.             }  
  75.   
  76.             //如果找到匹配的IE进程,则把当前InternetExplorer对象连接到正在运行的IE程序  
  77.             Console.WriteLine(DateTime.Now.ToString() + " 附加到IE");  
  78.             int i = 0;  
  79.             while (i < browsers.Count && ie == null)  
  80.             {  
  81.                 InternetExplorer e = browsers.Item(i) as InternetExplorer;  
  82.                 if (e != null)  
  83.                 {  
  84.                     if (e.HWND == (int)process.MainWindowHandle)  
  85.                     {  
  86.                         ie = e;  
  87.                         break;  
  88.                     }  
  89.                 }  
  90.                 ++i;  
  91.             }  
  92.             if (ie == null)  
  93.             {  
  94.                 Console.WriteLine(DateTime.Now.ToString() + " 附加到IE失败");  
  95.             }  
  96.             return ie;  
  97.         }  
  98.   
  99.         private static void Run(InternetExplorer ie)  
  100.         {  
  101.             ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);  
  102.   
  103.             try  
  104.             {  
  105.                 HTMLDocument document = null;  
  106.    
  107.                 object o = new object();  
  108.                 //导航到CSDN登录页  
  109.                 ie.Navigate("http://passport.csdn.net/UserLogin.aspx?from=http://community.csdn.net/"ref o, ref o, ref o, ref o);  
  110.                 documentComplete.WaitOne(1000, true);  
  111.   
  112.                 document = ie.Document as HTMLDocument;  
  113.                 if (document != null)  
  114.                 {  
  115.                     //以下操纵IE Shell  
  116.                     HTMLInputButtonElement name = document.getElementById("ctl00_CPH_Content_tb_LoginNameOrLoginEmail"as HTMLInputButtonElement;  
  117.                     if (name != null)  
  118.                     {  
  119.                         //csdn用户名  
  120.                         name.value = "yourcsdnusername";  
  121.                     }  
  122.                     HTMLInputButtonElement password = document.getElementById("ctl00_CPH_Content_tb_Password"as HTMLInputButtonElement;  
  123.                     if (password != null)  
  124.                     {  
  125.                         //csdn密码  
  126.                         password.value = "yourcsdnpassword";  
  127.                     }  
  128.   
  129.                     HTMLInputButtonElement imagecode = document.getElementById("ctl00_CPH_Content_tb_ExPwd"as HTMLInputButtonElement;  
  130.                     if (imagecode != null)  
  131.                     {  
  132.                         Console.Write("输入验证码:");  
  133.                         //控制台窗口输入验证码  
  134.                         string code = Console.ReadLine();  
  135.                         imagecode.value = code;  
  136.                     }  
  137.   
  138.                     HTMLInputButtonElement submit = document.getElementById("ctl00_CPH_Content_Image_Login"as HTMLInputButtonElement;  
  139.                     if (submit != null)  
  140.                     {  
  141.                         submit.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.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值