WEBBROWSER中模拟鼠标点击(SendMessage/PostMessage)
好久没有写文章,发一篇顶顶博客访问量。别人建议转一些比较好的代码也贴过来,但是我打算这里主要发自己原创的代码,所以么。。流量该多少就多少吧。。。
回到主题,在webbrowser中点击某链接网上几乎都是用document对象模拟点击,这个方法基本能应对一般的情况,但是例如广告联盟的点击XXX就有检测机制(不多解释,你们懂的)。所以完全模拟鼠标的点击事件就比较完美。于是我用了最常见的SendMessage。
接下来就要解决一个问题,webbrowser的句柄问题。从控件本身得到的句柄不是真正的浏览窗口的句柄,用SPY++看一下就能看出来,这里不贴图了。按照这个窗体的结构,用以下代码可以获取到网页的窗口的句柄。
'获得webbrowser的句柄
Private Function GetBrowserWindow(hWnd As Long) As Long
Dim lngHnd As Long
lngHnd = FindWindowEx(hWnd, 0, "Shell Embedding", vbNullString) '
lngHnd = FindWindowEx(lngHnd, 0, "Shell DocObject View", vbNullString)
lngHnd = FindWindowEx(lngHnd, 0, "Internet Explorer_Server", vbNullString)
GetBrowserWindow = lngHnd
End Function
然后就是网页元素的定位,向哪个坐标发送点击。这里用了DOM对象遍历来获取具体位置。都知道网页上一个元素有offsetLeft,offsetWidth,offsetHeight,offsetTop属性,但是都是相对容器来说的,所以可以通过遍历相加得到这个元素的绝对位置(这个绝对也是相对于网页浏览器窗口来说的。。)。于是代码如下:
Private Sub GetPos(objA As Object)
On Error Resume Next
adW = objA.offsetWidth
adH = objA.offsetHeight
adX = objA.offsetLeft
adY = objA.offsetTop
Set objA = objA.parentNode '遍历结点 获取绝对位置
Do While Not (objA Is Nothing)
adX = adX + objA.offsetLeft
adY = adY + objA.offsetTop
Set objA = objA.parentNode
Loop
txtX.Text = CStr(adX)
txtY.Text = CStr(adY)
'Debug.Print "X:" & adX, "Y:" & adY, "W:" & adW, "H:" & adH, "P:" & adPos
End Sub
好了,主要的问题分析完毕,我不多说废话了,直接贴代码看吧。
'获得webbrowser的句柄
Private Function GetBrowserWindow(hWnd As Long) As Long
Dim lngHnd As Long
lngHnd = FindWindowEx(hWnd, 0, "Shell Embedding", vbNullString) '
lngHnd = FindWindowEx(lngHnd, 0, "Shell DocObject View", vbNullString)
lngHnd = FindWindowEx(lngHnd, 0, "Internet Explorer_Server", vbNullString)
GetBrowserWindow = lngHnd
End Function
Private Function IsURL(objHTML As Object) As Boolean
On Error Resume Next
Dim strHTML As String, strURL As String
IsURL = False
strURL = LCase$(txtHost.Text)
strHTML = LCase$(objHTML.innerhtml) '都转成小写
If InStr(strHTML, strURL) > 0 Then IsURL = True '是这个域名 返回true
End Function
Private Sub GetPos(objA As Object)
On Error Resume Next
adW = objA.offsetWidth
adH = objA.offsetHeight
adX = objA.offsetLeft
adY = objA.offsetTop
Set objA = objA.parentNode '遍历结点 获取绝对位置
Do While Not (objA Is Nothing)
adX = adX + objA.offsetLeft
adY = adY + objA.offsetTop
Set objA = objA.parentNode
Loop
txtX.Text = CStr(adX)
txtY.Text = CStr(adY)
'Debug.Print "X:" & adX, "Y:" & adY, "W:" & adW, "H:" & adH, "P:" & adPos
End Sub
''获取坐标按钮点击事件
Private Sub cmdGetXY_Click()
On Error Resume Next
Dim objHTML As Object
Dim i As Integer
If txtHost.Text = "" Then
'MsgBox "不写域名,搞我呀。。。"
Exit Sub
End If
txtX.Text = ""
txtY.Text = ""
adX = 0
adY = 0
adW = 0
adH = 0
For i = 0 To 9
Set objHTML = webB.Document.GetElementByID("bdfs" & CStr(i))
If Not (objHTML Is Nothing) Then
If IsURL(objHTML) Then
Set objHTML = webB.Document.GetElementByID("dfs" & CStr(i))
adPos = 1 '右侧链接区
Call GetPos(objHTML)
Exit For
End If
End If
Set objHTML = webB.Document.GetElementByID("400" & CStr(i))
If Not (objHTML Is Nothing) Then
If IsURL(objHTML) Then
Set objHTML = webB.Document.GetElementByID("aw" & CStr(i - 1))
adPos = 0
Call GetPos(objHTML)
Exit For
End If
End If
Set objHTML = webB.Document.GetElementByID("300" & CStr(i))
If Not (objHTML Is Nothing) Then
If IsURL(objHTML) Then
Set objHTML = webB.Document.GetElementByID("aw" & CStr(i - 1))
adPos = 2
Call GetPos(objHTML)
Exit For
End If
End If
Next
'If adX = 0 And adY = 0 Then MsgBox "没有找到。。。"
Set objHTML = Nothing
End Sub
'''发送点击按钮点击事件
Private Sub cmdClick_Click()
On Error Resume Next
Dim x As Long, y As Long
Dim intRnd As Integer
Randomize '启动随机数
If adX = 0 And adY = 0 Then
'MsgBox "没有找到链接你也点。。。"
Exit Sub
End If
wbHwnd = GetBrowserWindow(Me.hWnd) '得到句柄
If adPos = 0 Then '在搜索结果区的上面
webB.Document.parentwindow.Scroll 0, adY - adH + 8 '修正下数据 正好对准
x = 30 + Int((Rnd * adW) / 2)
y = (Int((Rnd * adH) / 2) + 2) * &H10000
ElseIf adPos = 1 Then '在右侧的推广链接区
webB.Document.parentwindow.Scroll adX, adY - 11 '修正下数据
x = 150 + Int((Rnd * adW) / 2)
y = (Int((Rnd * adH) / 2) + 2) * &H10000
ElseIf adPos = 2 Then '在搜索结果当中
webB.Document.parentwindow.Scroll 0, adY - 11 '修正下数据
x = 30 + Int((Rnd * adW) / 2)
y = (Int((Rnd * adH) / 2) + 2) * &H10000
End If
'Debug.Print "Click:", x, y / &H10000
PostMessage wbHwnd, WM_LBUTTONDOWN, 1&, x + y
PostMessage wbHwnd, WM_LBUTTONUP, 1&, x + y
End Sub
有什么问题可以加我Q跟我讨论。
WEBBROWSER中模拟鼠标点击(SendMessage/PostMessage)的更多相关文章
- 使用powershell/vbs自动化模拟鼠标点击操作
今天想做windows上的自动化,所以才有了模拟鼠标点击的需求,先考虑用powershell实现: 首先先安装一个名为“WASP”免费可用的Powershell扩展程序,下载地址:http://was ...
- 利用python模拟鼠标点击自动完成工作,提升你的工作效率!
没有什么能比学以致用让学习变得更有动力的了. 不知道大家在工作中有没有一些工作需要重复的点击鼠标,因为会影响到财务统计报表的关系,我们每个月底月初都要修改ERP中的单据日期,单据多的时候光修改就能让你 ...
- C#实现模拟鼠标点击事件(点击桌面的其他程序 )
注释感觉已经很清楚了,有不懂的欢迎评论 1 using System; using System.Collections.Generic; using System.ComponentModel; u ...
- (原)python中matplot中获得鼠标点击的位置及显示灰度图像
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6182474.html 参考网址: http://matplotlib.org/examples/pyl ...
- C#用mouse_event模拟鼠标点击的问题
1.首先添加using System.Runtime.InteropServices; 2.为鼠标添加模拟点击的各种参数 //鼠标事件 因为我用的不多,所以其他参数没有写 1 2 3 4 5 6 7 ...
- jQuery模拟鼠标点击事件失效的问题
最近使用jQuery操作浏览器获取数据,需要对分页的信息进行处理,发现直接使用$('div#pager a.next').click();的这种写法无法触发点击事件. 使用trigger('click ...
- Webbrowser中模拟连接点击(非鼠标模拟)
Delphi uses mshtml, ActiveX; //初始加载网易主页 procedure TForm1.FormCreate(Sender: TObject); begin Webbrows ...
- 如何使用python来模拟鼠标点击(将通过实例自动化模拟在360浏览器中自动搜索"python")
一.准备工作: 安装pywin32,后面开发需要pywin32的支持,否则无法完成与windows层面相关的操作. pywin32的具体安装及注意事项: 1.整体开发环境: 基于windows7操作系 ...
- Qt 模拟鼠标点击(QApplication::sendEvent(ui->pushbutton, &event0);)
QPoint pos(0,0);QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt ...
随机推荐
- java集合类(六)About Queue
接上篇“java集合类(五)About Map” 终于来到了java集合类的尾声,太兴奋了,不是因为可以休息一阵了,而是因为又到了开启新知识的时刻,大家一起加油打气!!Come on...Fighti ...
- 【BZOJ】【1086】 【SCOI2005】王室联邦
树分块 orz vfk && PoPoQQQ http://vfleaking.blog.163.com/blog/static/174807634201231684436977/ h ...
- boost序列化
#include <iostream> #include <boost/serialization/serialization.hpp> #include <boost/ ...
- ios开发小技巧之摇一摇截屏
1. 监控摇一摇动作 1> 让当前视图控制器成为第一响应者 // 必须先让当前视图控制器成为第一响应者才能响应动作时间 [self becomeFirstResponder]; 2> 实现 ...
- Unity3D脚本中文系列教程(四)
http://dong2008hong.blog.163.com/blog/static/4696882720140302451146/ Unity3D脚本中文系列教程(三) 送到动画事件. ◆ va ...
- dom4j处理xml文件,读取xml字符串,格式化xml文件
1.xml文件 <?xml version="1.0" encoding="UTF-8"?> <employees> <emplo ...
- POJ2104 K-th Number Range Tree
又是区间第k大,这次选择这道题是为以后写线段树套平衡树铺路的.Range Tree可以理解成线段树套vector吧,相当于每个结点多存了对应区间的一个排好序的序列.画一下就会知道空间的消耗是nlogn ...
- gulp-css-spriter 雪碧图合成
一.配置 gulp的雪碧图功能没有grunt那么强大,但是类似功能也支持,功能稍微弱一些,但是也很棒 npm地址:https://www.npmjs.com/package/gulp-css-spri ...
- POJ 3286 How many 0's?
题目链接 题意 :写下m到n之间所有的数,会写多少个0. 思路 :先算0到m的,再算0到n的,最后相减. 网上有位大神是这么推的,看下面.... 首先转化成求 [0, x] 中所有数中,含有的 0 的 ...
- Android 虚拟机安装SD卡
在cmd命令行下,进入platform-tools目录下. 1.创建sdcard mksdcard -l mycard 256M E:\android\myCards\mysdcard.img ...