using System; using System.IO; using System.Linq; using SHDocVw; using mshtml;class Program { static void Main() { var q = from InternetExplorer proc in new ShellWindows() where Path.GetFileName(proc.FullName).ToLower().StartsWith("iexplore") select proc; var ie = q.FirstOrDefault(); if (ie == null) return; ie.Navigate("https://login.taobao.com/member/login.jhtml"); ie.DocumentComplete +=ie_DocumentComplete; Console.WriteLine("Press any key to exit"); Console.ReadKey(); } static void ie_DocumentComplete(object pDisp, ref object URL) { var ie = (SHDocVw.WebBrowser) pDisp; if (((string)URL).StartsWith("https://login.taobao.com/member/login.jhtml")) { var username = ((IHTMLDocument2) ie.Document).all.item("TPL_username_1") as IHTMLInputElement; username.value = "xxxxxx"; var password = ((IHTMLDocument2) ie.Document).all.item("TPL_password_1") as IHTMLInputElement; password.value = "xxx"; var submit = ((IHTMLDocument2) ie.Document).all.item("J_SubmitStatic") as IHTMLElement; submit.click(); } } }
那就用模拟鼠标点击好了:using System; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; using SHDocVw; using mshtml;class Program { [STAThread] static void Main() { var ie = new ShellWindows().OfType<InternetExplorer>() .FirstOrDefault(w => Path.GetFileName(w.FullName).ToLower().StartsWith("iexplore")); if (ie == null) return; var doc = ie.Document as IHTMLDocument3; var link = doc.getElementsByTagName("a").OfType<IHTMLElement>() .FirstOrDefault(a => a.innerText == "登录") as IHTMLElement2; if (link == null) return;
var rect = link.getBoundingClientRect(); // client rectangle var win = ((IHTMLDocument2)doc).parentWindow as IHTMLWindow3; var pt = new Point(rect.left + win.screenLeft, rect.top + win.screenTop); pt.Offset((rect.right-rect.left)/2, (rect.bottom-rect.top)/2); ShowWindow((IntPtr)ie.HWND, SW_SHOW); SetForegroundWindow((IntPtr) ie.HWND); Thread.Sleep(1000);
var cursor = new Cursor(Cursor.Current.Handle); Cursor.Position = pt;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)pt.X, (uint)pt.Y, 0, 0); } [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo); [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("user32")] public static extern int SetForegroundWindow(IntPtr hwnd); private const int SW_SHOW = 5; private const int MOUSEEVENTF_LEFTDOWN = 0x02; private const int MOUSEEVENTF_LEFTUP = 0x04; }
或者直接类似这个的获取"登录"这个元素的屏幕坐标的代码,找图之类虽然速度挺快,毕竟维护起来不靠谱,
using System;
using System.IO;
using System.Linq;
using SHDocVw;
using mshtml;class Program
{
static void Main()
{
var q = from InternetExplorer proc in new ShellWindows()
where Path.GetFileName(proc.FullName).ToLower().StartsWith("iexplore")
select proc; var ie = q.FirstOrDefault();
if (ie == null)
return; ie.Navigate("https://login.taobao.com/member/login.jhtml");
ie.DocumentComplete +=ie_DocumentComplete; Console.WriteLine("Press any key to exit");
Console.ReadKey();
} static void ie_DocumentComplete(object pDisp, ref object URL)
{
var ie = (SHDocVw.WebBrowser) pDisp;
if (((string)URL).StartsWith("https://login.taobao.com/member/login.jhtml"))
{
var username = ((IHTMLDocument2) ie.Document).all.item("TPL_username_1") as IHTMLInputElement;
username.value = "xxxxxx";
var password = ((IHTMLDocument2) ie.Document).all.item("TPL_password_1") as IHTMLInputElement;
password.value = "xxx";
var submit = ((IHTMLDocument2) ie.Document).all.item("J_SubmitStatic") as IHTMLElement;
submit.click();
}
}
}
?spm=......,用程序click事件出来的没spm码,手动在浏览器点击的有spm码,或者手动右键链接后,链接也自动加上了spm码
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using SHDocVw;
using mshtml;class Program
{
[STAThread]
static void Main()
{
var ie = new ShellWindows().OfType<InternetExplorer>()
.FirstOrDefault(w => Path.GetFileName(w.FullName).ToLower().StartsWith("iexplore"));
if (ie == null)
return; var doc = ie.Document as IHTMLDocument3;
var link = doc.getElementsByTagName("a").OfType<IHTMLElement>()
.FirstOrDefault(a => a.innerText == "登录") as IHTMLElement2; if (link == null)
return;
var rect = link.getBoundingClientRect(); // client rectangle
var win = ((IHTMLDocument2)doc).parentWindow as IHTMLWindow3;
var pt = new Point(rect.left + win.screenLeft, rect.top + win.screenTop);
pt.Offset((rect.right-rect.left)/2, (rect.bottom-rect.top)/2); ShowWindow((IntPtr)ie.HWND, SW_SHOW);
SetForegroundWindow((IntPtr) ie.HWND);
Thread.Sleep(1000);
var cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = pt;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)pt.X, (uint)pt.Y, 0, 0);
} [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo); [DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("user32")]
public static extern int SetForegroundWindow(IntPtr hwnd); private const int SW_SHOW = 5;
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
}
我这边也是用 element.click(),来触发的,进店的来源还是 直接进店,然后 用 模拟鼠标点击(跟帖子里面的实现差不多,多了个鼠标的随机移动,我以为是要触发mouse_move)的,也是直接进店。
不知道你最后怎么解决的?感觉正如你说的,是淘宝在脚本懂了手脚,还希望你能指导下我,非常感谢啊。
这问题困扰我三个多星期了。谢谢啊
SPM时淘宝用来跟踪买家行为的,所以他必须要触发到。