实例介绍
【实例截图】
【核心代码】
using CefSharp; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using TestClick.common; namespace TestClick { public partial class Form1 : Form { [DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32")] private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);//鼠标事件 const int MOUSEEVENTF_MOVE = 0x0001; //移动鼠标 const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下 const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起 const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下 const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起 const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键按下 const int MOUSEEVENTF_MIDDLEUP = 0x0040; //模拟鼠标中键抬起 const int MOUSEEVENTF_ABSOLUTE = 0x8000; //标示是否采用绝对坐标 [DllImport("user32.dll")] private static extern int GetWindowRect(IntPtr hwnd, out Rect lpRect); [DllImport("user32.dll", EntryPoint = "SendMessage")] public static extern int PostMessage(IntPtr 句柄,int 事件,int 参数1,int 参数2); public Form1() { InitializeComponent(); } public CefSharp.WinForms.ChromiumWebBrowser webBrowser1; string url = ""; string cookie_str = ""; private void Form1_Load(object sender, EventArgs e) { url = "https://www.taobao.com/"; webBrowser1 = new CefSharp.WinForms.ChromiumWebBrowser(url); this.panel1.Controls.Add(webBrowser1); //this.Controls.Add(webBrowser1); BrowserSettings browserSettings = new BrowserSettings(); browserSettings.FileAccessFromFileUrls = CefState.Enabled; browserSettings.UniversalAccessFromFileUrls = CefState.Enabled; browserSettings.WebSecurity = CefState.Enabled; webBrowser1.BrowserSettings = browserSettings; webBrowser1.Dock = DockStyle.Fill;//铺满 webBrowser1.AddressChanged = WebBrowser1_AddressChanged; webBrowser1.FrameLoadEnd = WebBrowser1_FrameLoadEnd; } private void WebBrowser1_AddressChanged(object sender, AddressChangedEventArgs e) { url = e.Address.ToString(); //if (e.Address.IndexOf("http://www.miandoudou.com/mddapp/pdd/open.php") == 0) //{ CookieVisitor visitor = new CookieVisitor(); visitor.SendCookie = visitor_SendCookie; var cookieManager = CefSharp.Cef.GetGlobalCookieManager(); cookieManager.VisitAllCookies(visitor); //webBrowser1.Load("http://www.miandoudou.com/mddapp/pdd/open.php"); //} } public class CookieVisitor : CefSharp.ICookieVisitor { public event Action<CefSharp.Cookie> SendCookie; public void Dispose() { //throw new NotImplementedException(); } public bool Visit(CefSharp.Cookie cookie, int count, int total, ref bool deleteCookie) { deleteCookie = false; if (SendCookie != null) { SendCookie(cookie); } return true; } } private void visitor_SendCookie(CefSharp.Cookie obj) { System.Net.Cookie ck = new System.Net.Cookie(obj.Name, obj.Value); ck.Domain = obj.Domain; cookie_str = obj.Name "=" obj.Value "; "; } private void WebBrowser1_FrameLoadEnd(object sender, FrameLoadEndEventArgs e) { //if (e.Browser.MainFrame.Url.Contains("https://mms.pinduoduo.com/login") && !string.IsNullOrEmpty(username)) //{ // e.Browser.MainFrame.ExecuteJavaScriptAsync("document.getElementById('usernameId').value='" username "'"); //} } private void btn_tiaozhuan_Click(object sender, EventArgs e) { string url1 = "https://item.taobao.com/item.htm?id=" this.txt_id.Text.Trim(' '); webBrowser1.Load(url1); } /// <summary> /// 点击测试 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_test_Click(object sender, EventArgs e) { string backhtml = HttpHelper.HttpGet(url, "", "gbk", cookie_str); List<string> Li_list = Helper.str_FindStr(backhtml, "<li data-value=", "</li>", 0, true, false, false, "", "", "", ""); string yanse = this.txt_yanse.Text.Trim(' '); string chima=this.txt_chima.Text.Trim(' '); Task<CefSharp.JavascriptResponse> t = webBrowser1.EvaluateScriptAsync("document.getElementsByTagName('li').length"); string length_ = ""; t.Wait(); if (t.Result.Result != null) { length_=t.Result.Result.ToString(); } t.Dispose(); string sel_state = ""; int count_ = 0; if (Li_list.Count > 0) { //点击颜色 和尺码 foreach (var item in Li_list) { sel_state = ""; string ys = Helper.GetZhongjianStr(item, "<span>", "</span>", "", "", 1, 1); if (ys == yanse || ys == chima) { string htmlid = Helper.GetZhongjianStr(item, "\"", "\"", "", "", 1, 1); if (length_ != "") { for (int i = 0; i < Convert.ToInt32(length_); i ) { sel_state = ""; Task<CefSharp.JavascriptResponse> t1 = webBrowser1.EvaluateScriptAsync("document.getElementsByTagName('li')[" i "].getAttribute('data-value')"); t1.Wait(); Task<CefSharp.JavascriptResponse> t2 = webBrowser1.EvaluateScriptAsync("document.getElementsByTagName('li')[" i "].getAttribute('class')"); t2.Wait(); if (t2.Result.Result != null) { sel_state = t2.Result.Result.ToString(); } if (t1.Result.Result != null) { if (htmlid == t1.Result.Result.ToString()) { if (sel_state != "tb-selected") { count_ ; webBrowser1.EvaluateScriptAsync("document.getElementsByTagName('li')[" i "].click()"); } //MessageBox.Show(i.ToString()); //break; } } t1.Dispose(); t2.Dispose(); } } } } if(count_ > 0)//执行点击购买操作 { Thread.Sleep(500); string a_length = ""; Task<CefSharp.JavascriptResponse> t5 = webBrowser1.EvaluateScriptAsync("document.getElementsByTagName('a').length"); t5.Wait(); if (t5.Result.Result != null) { a_length = t5.Result.Result.ToString(); } if (a_length != "") { for (int i = 0; i < Convert.ToInt32(a_length); i ) { Task<CefSharp.JavascriptResponse> t4 = webBrowser1.EvaluateScriptAsync("document.getElementsByTagName('a')[" i "].getAttribute('shortcut-label')"); t4.Wait(); if (t4.Result.Result != null) { if (t4.Result.Result.ToString() == "立即购买") { webBrowser1.EvaluateScriptAsync("document.getElementsByTagName('a')[" i "].click()"); break; } } } } t5.Dispose(); } } } private void button1_Click(object sender, EventArgs e) { IntPtr a = FindWindow(null, "测试点击"); //PostMessage(a, MOUSEEVENTF_MOVE,10, 10); //mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 10500, 10500,0, 0); //Rect lpRect = new Rect(); //GetWindowRect(a, out lpRect); //int kuan = lpRect.Width; //int gao = lpRect.Height; //int x = 50 * 65535 / kuan; //int y = 50 * 65535 / gao; //mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0); // Dm.dmsoft dm = new Dm.dmsoft(); //int jubing = dm.FindWindow(null, "测试点击"); //int jubing = dm.FindWindow("CefBrowserWindow",null); //if (jubing == 0) //{ // return; //} //int abc=dm.BindWindow(jubing, "gdi", "windows", "windows", 0);//绑定窗口句柄 //MessageBox.Show(abc.ToString()); //Thread.Sleep(1000); // dm.MoveTo(50,50); } } }
标签:
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论