如何自动处理弹出框

qqx3502 2008-07-23 07:24:15
说明:程序执行自动填表时,程序"点击"了提交按钮后,网页会自动弹出个确认框为"确认无误提交吗?",只有点击了确认框上的确认按钮后才能完成整个提交,网页上的提交按钮可以查找"submit"很容易搞定,但弹出框里面的确认按钮不知如何搞定,请大家帮忙分析,我想可不可以用SendKeys来搞定?谢谢!!!

弹出框的源码(找了半天好像就一句):
else if (window.confirm("确认无误提交吗?"))

我用SendKeys写了,但运行时有问题,是不是还需要引用什么?代码如下:
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 500
WshShell.SendKeys "{ENTER}"
...全文
284 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzyong00 2008-07-27
  • 打赏
  • 举报
回复
Option Explicit

Private Declare Function FindWindow _
Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As Any, _
ByVal lpWindowName As String) As Long

Private Declare Function SendMessage _
Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

'在窗口列表中寻找与指定条件相符的第一个子窗口
Private Declare Function FindWindowEx _
Lib "user32" _
Alias "FindWindowExA" (ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

Private Const BM_CLICK As Long = &HF5

Private Sub Command1_Click()
Dim h1 As Long, h2 As Long
h1 = FindWindow(0&, "Windows Internet Explorer")
h2 = FindWindowEx(h1, 0, "Button", "确定")
SendMessage h2, BM_CLICK, 0, ByVal 0&
SendMessage h2, BM_CLICK, 0, ByVal 0&
End Sub
fvflove 2008-07-27
  • 打赏
  • 举报
回复

'接上,将一个新的程序设置成这样.
'在用的时候,注意,一定要先运行此程序.
'在运行你那个要自动单击的程序的时候,一定要让那个程序在最上方(窗口的最前方)
Private Const LB_DELETESTRING = &H182
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type


Private Const LB_ADDSTRING = &H180

Const GW_HWNDFIRST = 0
Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2
Const GW_HWNDPREV = 3
Const GW_OWNER = 4
Const GW_CHILD = 5
Const GW_MAX = 5

Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long '这个是设置鼠标的位置!

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long) '定义鼠标事件


Dim chWnd As Long
Dim listHwnd As Long

Private Sub Timer1_Timer()
Dim chGwnd As Long
Dim clsname As String * 256
chWnd = FindWindow(vbNullString, "Microsoft Internet Explorer") '"fvflove"改为窗口的名称要用全称
chGwnd = GetWindow(chWnd, GW_CHILD)
GetClassName chGwnd, clsname, 256
Label1.Caption = clsname
If InStr(1, clsname, "Button") Then
listHwnd = chGwnd '此处是返回的TextBox的句柄 ,你可以放入一个集合或数组
GoTo LAA
End If
Label1.Caption = clsname
While chGwnd <> 0
Label1.Caption = clsname
chGwnd = GetWindow(chGwnd, GW_HWNDNEXT)
GetClassName chGwnd, clsname, 256
If InStr(1, clsname, "Button") Then
listHwnd = chGwnd '此处是返回的TextBox的句柄 ,你可以放入一个集合或数组
GoTo LAA
End If
Wend
Me.Caption = listHwnd

LAA:
If listHwnd <> 0 Then
Dim lpRect As RECT
GetWindowRect listHwnd, lpRect
Print lpRect.Left
Print lpRect.Top
Print lpRect.Bottom
Print lpRect.Right

Call SetCursorPos(lpRect.Left + (lpRect.Right - lpRect.Left) / 2, lpRect.Top + (lpRect.Bottom - lpRect.Top) / 2)
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Timer1.Enabled = False '此处设置只执行单击一次事件.
End If
End Sub


zzyong00 2008-07-27
  • 打赏
  • 举报
回复
[Quote=引用楼主 qqx3502 的帖子:]
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 500
WshShell.SendKeys "{ENTER}"
[/Quote]
像这样,发送一个回车按键,但只是发向拥有焦点的窗口,拥有焦点煌窗口是弹出的哪个窗口吗?
你在程序中可以这样:

AppActivate "Windows Internet Explorer" '激活哪个弹出窗口,引号中是哪个窗口的标题
DoEvents'让弹出窗口有时间反应--激活自己
SendKeys "{ENTER}" '发送按键


在我这里测试通过
方法二:用API,如fvflove 是直接将弹出窗口关闭,没有选择是与否,但效果也是可以掉窗口的
再说是FindWindow后,用FindWindowEx找到哪个“是”按钮的句柄,发BM_CLICK消息过去



以上两方法只是表面的,要从根本上解决问题,还得是如蒋晟所说,要实现IDocHostShowUI 接口
fvflove 2008-07-27
  • 打赏
  • 举报
回复
再来试一下.
qqx3502 2008-07-27
  • 打赏
  • 举报
回复
to:fvflove
那我调试咋就不成功呢?真郁闷,我都试了一个上午了(换其他窗口都行就是对Microsoft Internet Explorer窗口不行),还有个问题就是即使成功好像只是关闭了弹出框,其效果应该等同于点击弹出框右上角的关闭按钮吧!这样就不会完成投注的动作了呀!我想实现的是点击弹出框的确定按钮完成投注并退出弹出框,又想了个方法是不是可以新建个程序,他可以让投注程序窗口获得焦点,再模仿键击(即用sendkeys "{ENTER}")或许行~~
fvflove 2008-07-27
  • 打赏
  • 举报
回复

'建立一个程序
'以下是程序中的代码 此程序只有一个Timer1的控件.
'Timer1.Enabled = True
'Timer1.Interval = 100
'在你运行你那个自运投注的程序的时候,先让此程序运行.
'以下代码经过调试,成功

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_CLOSE = &H10

Private Sub Timer1_Timer()
Dim hwnd
hwnd = FindWindow(0&, "Microsoft Internet Explorer") '此处你为什么要用 "FORM1" 为什么不用
SendMessage hwnd, WM_CLOSE, 0&, 0&
End Sub
蒋晟 2008-07-27
  • 打赏
  • 举报
回复
用IDocHostShowUI
qqx3502 2008-07-27
  • 打赏
  • 举报
回复
非常感谢大家热情的分析与讨论,用双程序,虽然麻烦了点,但问题解决啦,现在不明白的就是怎样实现IDocHostShowUI 接口,感觉挺深的,暂时不去研究,等空下来在慢慢研究@@
qqx3502 2008-07-26
  • 打赏
  • 举报
回复
突然想到:可不可以用多线程!!!
fvflove 2008-07-26
  • 打赏
  • 举报
回复
[CODE=VB]
代码2:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_CLOSE = &H10

Private Sub Command1_Click()
Dim hwnd
MsgBox "下课了!"
hwnd = FindWindow(0&, "FORM1") '此处你为什么要用 "FORM1" 为什么不用 MsgBox "下课了!" 弹出的对话框的标题?
SendMessage hwnd, WM_CLOSE, 0&, 0&
End Sub
'运行程序卡在弹出框位置,手动关闭弹出框,程序才关闭FORM1窗口!


'如果以上代码打弹出框关了.
'再关闭整个窗口就不用我再说了.
[/CODE]

qqx3502 2008-07-26
  • 打赏
  • 举报
回复
也还是不行,找了window.confirm的MSDN,地址如下:
http://msdn.microsoft.com/zh-cn/netframework/system.windows.forms.htmlwindow.confirm(VS.85).aspx
我也看不出什么名堂,请大家看看,上面说:
参数
message
返回值
如果用户单击“是”,则为 true;如果用户单击“否”或关闭对话框,则为 false。
我们是不是要message一个true不就OK拉,但怎么message呢?
zzyong00 2008-07-26
  • 打赏
  • 举报
回复
appactivate 对话框标题
sendkey "{ENTER}"
qqx3502 2008-07-26
  • 打赏
  • 举报
回复
还是不行,弹出框跳出后下面的程序就停止了,为了验证,我写了下面的代码1,运行OK,加了弹出框代码2,运行失败!
代码1:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Sub Command1_Click()
Dim hwnd
hwnd = FindWindow(0&, "FORM1")
SendMessage hwnd, WM_CLOSE, 0&, 0&
End Sub
运行成功关闭FORM1窗口!

代码2:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_CLOSE = &H10

Private Sub Command1_Click()
Dim hwnd
MsgBox "下课了!"
hwnd = FindWindow(0&, "FORM1")
SendMessage hwnd, WM_CLOSE, 0&, 0&
End Sub
运行程序卡在弹出框位置,手动关闭弹出框,程序才关闭FORM1窗口!
fvflove 2008-07-26
  • 打赏
  • 举报
回复



Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_CLOSE = &H10


'先用
Hwnd=FindWindow(0&,"要关闭的窗口标题(注意一定要完整的)")

'再发送关闭信息

sendMessage Hwnd , WM_Close ,0&,0&




'以上代码未经过调试,.但是大概是这个样子.

fvflove 2008-07-26
  • 打赏
  • 举报
回复
你可去找到弹出框的句柄,再发送关闭信息啊.
qqx3502 2008-07-26
  • 打赏
  • 举报
回复
现在知道用SendKeys为什么不行了,是因为程序执行到弹出框弹出后就停止执行下面的代码了,也就是说程序挂起来了,所以无法执行SendKeys命令,必须手动点击确定按钮程序才执行下面的语句,如果真的这样那各位大侠的其他方法不也是没办法执行吗?问题大啦~~~

不知用什么可以把挂起的程序解挂!!!
fj182 2008-07-25
  • 打赏
  • 举报
回复
还有一种办法你可以尝试一下,找到要提交的Form,把onSubmit 设为 null。如:

set objDoc = webbrowser.document
set objform = objdoc.forms("testForm")

objform.onsubmit = null
fvflove 2008-07-25
  • 打赏
  • 举报
回复
能不能给出弹出对话框的网址,我试一下看行不先.
qqx3502 2008-07-25
  • 打赏
  • 举报
回复
to:FU0212
怎样FINDWINDOW到这个窗口 怎样送一个按键消息 SENDMESSAGE啊?能不能给个代码看看,谢谢!!!
spotty2008 2008-07-25
  • 打赏
  • 举报
回复
大哥 这是VBSCRIP 不要弄混了
加载更多回复(6)

7,787

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧