2014年12月28日
C#实现自动领取淘金币
起初本想通过httphelper来实现自动领取淘金币的,但是我认为以自己目前的水平还没有能力实现这个功能,所以最终采用了C#模拟键盘鼠标的方式实现了该功能。首先我尝试用httphelper尝试实现了自动登录淘宝,可是若要用同样的方法实现自动领取淘金币,我必须找到领取淘金币的URL中所提交的关键信息,在我的认知里,这些关键信息必须调用一个基于KISSY库的js文件才能获取,这个功能我暂不知该如何实现。本着先易后难,条条大路通罗马的原则,我先尝试实现通过C#模拟键盘与鼠标的动作来实现该功能。
通过C#模拟键盘与鼠标的动作来实现自动领取淘金币(若出现验证码则需手动输入)的步骤如下:
1.通过mshtml与SHDocVw模拟键盘输入自动登录淘宝;
2.登录完成后,打开淘金币页面;
3.模拟鼠标点击领取淘金币;
4.若无需输入验证码,则可成功领取淘金币;若需要输入验证码,需手动输入。
代码如下,请多多指教。
代码运行注意事项:1.首次运行该代码时,若当前系统下的默认浏览器不是IE,则程序运行会出错,请参考这里;这时请设置默认浏览器为IE,然后再运行程序则可成功,运行成功一次后,后续将默认浏览器更改为非IE浏览器,程序也将不会报错。
2.淘宝登录界面的密码框似乎不接受setAttribute的方式输入,目前我尝试成功的是sendKeys的方式。但sendKeys只能在有form的前提下,因此建立项目时需要选择windows应用窗体的类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using mshtml; using SHDocVw;//Microsoft Internet controls using System.Runtime.InteropServices; using System.Threading; namespace autoSearchBaidu { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //1.开启一个新的IE窗口 SHDocVw.InternetExplorer ieWnd = new SHDocVw.InternetExplorer(); object missing = null; mshtml.IHTMLDocument2 ieDoc; ieWnd.Visible = true; //2.在新的IE窗口中打开淘宝登录界面 ieWnd.Navigate("http://login.taobao.com/", ref missing, ref missing, ref missing, ref missing); //3.等待页面加载完成 while (ieWnd.Busy || ieWnd.ReadyState != tagREADYSTATE.READYSTATE_COMPLETE) { Thread.Sleep(500); } //4.模拟键盘在相应位置输入用户名与密码并提交 ieDoc = (mshtml.IHTMLDocument2)ieWnd.Document; mshtml.HTMLInputElementClass input; foreach (mshtml.IHTMLElement ieElement in ieDoc.all) { if (ieElement.tagName.ToUpper().Equals("INPUT")) { input = ((mshtml.HTMLInputElementClass)ieElement); //"TPL_username"登录界面中用户名输入框的name if (input.name == "TPL_username") { input.setAttribute("value", "", 0);//防止登录界面有默认用户名,先将用户名清空 input.focus(); //用户名可以用setAttribute的方式来设置,但密码不接受这种输入,而且密码框不可以focus,因此用户名与密码均用键盘模拟输入 System.Windows.Forms.SendKeys.Send("用户名"); System.Windows.Forms.SendKeys.Send("{tab}"); System.Windows.Forms.SendKeys.Send("密码"); break; } } } mshtml.HTMLButtonElementClass SubmitBtn; foreach (mshtml.IHTMLElement ieElement in ieDoc.all) { if (ieElement.tagName.ToUpper().Equals("BUTTON")) { //登录界面中仅有一个Button,故直接对该Button执行click动作 SubmitBtn = (mshtml.HTMLButtonElementClass)ieElement; SubmitBtn.click(); break; } } //5.等待登录页面加载完成 while (ieWnd.Busy || ieWnd.ReadyState != tagREADYSTATE.READYSTATE_COMPLETE) { Thread.Sleep(500); } //6.在IE上打开淘金币页面 ieWnd.Navigate("http://taojinbi.taobao.com/", ref missing, ref missing, ref missing, ref missing); //7.等待登录页面跳转完成 while (ieWnd.Busy || ieWnd.ReadyState != tagREADYSTATE.READYSTATE_COMPLETE) { Thread.Sleep(500); } //8.获取“领取淘金币”的元素信息,并触发点击事件。因页面中a链接较多,故此处需要等待较长时间。若需要验证码,则需手动输入。 ieDoc = (mshtml.IHTMLDocument2)ieWnd.Document; mshtml.HTMLAnchorElementClass aCoin; foreach (mshtml.IHTMLElement ieElement in ieDoc.all) { if (ieElement.tagName.ToUpper().Equals("A")) { aCoin = ((mshtml.HTMLAnchorElementClass)ieElement); //此处必须先判断当前元素有className才可以判断其名称值,否则会报错 if (aCoin.className != null) { if (aCoin.className.Trim() == "btn login-btn J_GoTodayBtn") { aCoin.click(); } } } } } } } |