对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教 Topic is solved

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

22 Sep 2016, 09:41

网页中有一个保存按钮,点击保存按钮会弹出一个确认对话框,对话框中有一个确认按钮,我想实现按F2按钮后自动点击保存按钮并对弹出的对话框自动确认,下面脚本按F2能弹出对话框,却一直无法自动对弹出的对话框自动确认,请求tmplinshi及各位大侠指教
点击保存按钮后调用的save函数如下:

Code: Select all

function save(){
		if(confirm("确认保存?")){
			document.frm.action="….”; 
			document.frm.submit();
		}
		resetDisabledButton();
}
使用Active window info 对弹出的确认对话框的信息获取如下
Window Title:
来自网页的消息
ahk_class #32770
ahk_exe iexplore.exe
Control Under Mouse Position:
ClassNN: Button1
Text: 确定
Color: 000000 (Red=00 Green=00 Blue=00)

我写的脚本如下

Code: Select all

SetTitleMatchMode Regex

gui +lastfound
hwnd := winexist()
dllcall( "RegisterShellHookWindow", uint,hwnd )
msgnum := dllcall( "RegisterWindowMessage", Str,"SHELLHOOK" )
onmessage( msgnum, "shellmessage" )
return

shellmessage(wparam,lparam) {
    if (wparam = 32772) && winexist("来自网页的消息")
        ControlClick, Button1, 来自网页的消息, 确认保存?
}

F2::
	try{
		Pwb := WBGet()
		Pwb.document.getElementById("saveButton").click()
		' ControlClick, Button1, 来自网页的消息, 确认保存?
	}
return
Pwb.document.getElementById("saveButton").click()下面的controlClick 没有对弹出的对话框响应之前根本无法执行,所以想使用onmessage() 响应弹出窗口消息,却一直无法实现,请求tmplinshi及各位大侠指教
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

22 Sep 2016, 10:19

你好,我有一个测试htm文件,但不知如何上传

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html>  
<head>
<meta http-equiv="content-type" content="text/html;charset=GBK"/>    
<title>申报系统</title>
<link href="/BizforBankWeb/css/style.css" rel="stylesheet" type="text/css"/>
<link href="/BizforBankWeb/css/calendar.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<form id="frm" name="frm" method="post" action="">
<!-- 导航条 -->
<table width="98%" valign="top" align="center" border="0" cellpadding="0" cellspacing="0">
	<tr class="text01">
        <td height="18" valign="middle" id="positionininfo" nowrap><img src="/BizforBankWeb/images/currentpositionbg.jpg" width="8" height="18" id="positionimg"/><div class="currentposition"><strong>&nbsp;当前位置:</strong></div></td>
        <td align="right" >
	    	<input type="button" id="saveButton" name="saveButton" value="确认(S)" onclick="save();" Onkeydown="_afterEnter();" class="button" accesskey="S" tabindex="500" />
      		<input type="button" id="returnButton" name="returnButton" value="返回(R)" onclick="back();" Onkeydown="_afterEnter();" class="button" accesskey="R"  tabindex="600"/>
      	</td>
    </tr>
</table>
<div id="msg" style="display:none;"></div>

	
</body>
</form>
</html>

<script language="JavaScript" type="text/javascript">

function save(){
		if(confirm("确认保存?")){
			document.frm.action="/BizforBankWeb/servlet/raReportDataAuditServlet?time="+new Date(); 
			document.frm.submit();
		}
		resetDisabledButton();
}


function clickChgSourceFlagCheckBox(obj){

	if(obj.checked){
		chgSourceFlag.value=1;
		document.getElementById("commentDiv").style.display="block";

	}
}
</script>
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教  Topic is solved

22 Sep 2016, 22:21

测试网页

Code: Select all

#NoEnv
#Persistent
SetBatchLines, -1
DetectHiddenWindows, On

EVENT_SYSTEM_ALERT := 0x0002
hook := New WinEventHook(EVENT_SYSTEM_ALERT, EVENT_SYSTEM_ALERT, "HookProc")
Return

HookProc(hWinEventHook, event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime) {
	; 如果没有 DetectHiddenWindows On,则需要进行 WinWait
	; WinWait, ahk_id %hWnd%
	
	WinGetTitle, title, ahk_id %hWnd%
	if (title = "来自网页的消息") {
		ControlSend,, {Enter}, ahk_id %hWnd%
	}
}

class WinEventHook {
	__New(eventMin, eventMax, funcName) {
		DllCall("Ole32.dll\CoInitialize", "Ptr", 0)

		this.hWinEventHook := DllCall("SetWinEventHook"
			, "UInt", eventMin
			, "UInt", eventMax
			, "Ptr", 0
			, "Ptr", RegisterCallback(funcName, "F")
			, "UInt", 0
			, "UInt", 0
			, "UInt", 0x0)
	}

	__Delete() {
		DllCall("UnhookWinEvent", "Ptr", this.hWinEventHook)
		DllCall("Ole32.dll\CoUninitialize")
	}
}
Last edited by tmplinshi on 23 Sep 2016, 03:06, edited 1 time in total.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

22 Sep 2016, 23:32

还有一种方法是把 confirm 函数替换掉:

Code: Select all

wb := WBGet()
wb.document.parentWindow.execScript("function confirm(){return true}")
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

22 Sep 2016, 23:57

tmplinshi大神,您太牛了,向您膜拜!!!autohotkey没有您解决不了的事,我每次碰到问题都花很多时间上网查找解决方法,最后都是您帮忙解决,再次感谢!!!!!!
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 03:10

弄错了一个变量名。SetWinEventHook 的第二个参数应该是 eventMax,而我写成了 eventMin。代码已修改。
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 06:16

tmplinshi大神,能否解释一下我写的代码失败的原因,和您的代码实现的原理,让大家学习一下。
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 06:17

请教一下,论坛如何上传图片等附件?
Last edited by gongnl on 23 Sep 2016, 06:36, edited 1 time in total.
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 06:32

我把F2改为 gui 定义的一个按钮调用就不成功了什么原因?

Code: Select all

Gui, Add, Button,x266 y6 w48 h26,确认 
...
Button确认:
	try{
		Pwb := WBGet()
		Pwb.document.getElementById("saveButton").click()
	}
return
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 09:22

wb.document.parentWindow.execScript("function confirm(){return true}") 我试了好像无效,不会调用 网页内的save()函数
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 09:33

你顶楼的代码还要加上等待“来自网页的消息”窗口出现才能成功。

32772 是 HSHELL_RUDEAPPACTIVATED 事件,在窗口激活时产生。最好是用另外一个消息 HSHELL_WINDOWCREATED,从名字可知道是窗口创建时产生。

Code: Select all

#NoEnv
#Persistent
SetBatchLines, -1

DllCall("RegisterShellHookWindow", "ptr", A_ScriptHwnd)
msgNum := DllCall("RegisterWindowMessage", "str", "SHELLHOOK")
OnMessage(msgNum, "ShellProc")
return

ShellProc(wParam, lParam) {
	if (wParam = 1) { ; HSHELL_WINDOWCREATED = 1
		WinGetClass, className, ahk_id %lParam%
		if (className = "Alternate Modal Top Most") {
			WinWait, 来自网页的消息
			ControlSend,, {Enter}
		}
	}
}
个人认为这个方法没有 WinEventHook 的方法好,毕竟这是监测窗口创建,而 WinEventHook 可以只监测 EVENT_SYSTEM_ALERT 消息。
图片可以上传到 http://upload.otar.im/
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 09:39

gongnl wrote:wb.document.parentWindow.execScript("function confirm(){return true}") 我试了好像无效,不会调用 网页内的save()函数
这句代码的作用是改写 confirm 函数,让它直接返回 true。这样当网页调用了 confirm 时就会直接得到选择了【确定】的结果,而达到了你标题“弹出的对话框自动确认”的目的。

我上面写的三种方法都是只实现“弹出的对话框自动确认”。如果要调用 save() 函数,试试这样:

Code: Select all

wb.document.parentWindow.execScript("save()")
Last edited by tmplinshi on 23 Sep 2016, 09:48, edited 1 time in total.
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 09:47

tmplinshi wrote:
gongnl wrote:wb.document.parentWindow.execScript("function confirm(){return true}") 我试了好像无效,不会调用 网页内的save()函数
这句代码的作用是改写 confirm 函数,让它直接返回 true。这样当网页调用了 confirm 时就会直接得到选择了【确定】的结果,而达到了你标题“弹出的对话框自动确认”的目的。

我上面写的三种方法都是只实现“弹出的对话框自动确认”。
我使用execScript("function confirm(){return true}") 试了根本没有 反应,并且 confirm()后面的{document.frm.action="….”; document.frm.submit();} 也执行不了
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 09:52

gongnl wrote: 我使用execScript("function confirm(){return true}") 试了根本没有 反应,并且 confirm()后面的{document.frm.action="….”; document.frm.submit();} 也执行不了
确实不会有任何反应。这个代码只是改写了 confirm() 函数,只有在网页调用 confirm() 函数时才有效果。比如没有改写之前 confirm("确认保存?") 会弹出一个对话框,而改写后这个对话框就不会弹出来了,直接会收到【确定】的结果。
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 09:58

wb.document.parentWindow.execScript("function confirm(){return true}")
需要注意的是,这个代码是修改网页源码。也就是要等网页打开后再执行,并且如果你刷新了网页也需要再次执行。
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 10:10

tmplinshi wrote:
gongnl wrote: 我使用execScript("function confirm(){return true}") 试了根本没有 反应,并且 confirm()后面的{document.frm.action="….”; document.frm.submit();} 也执行不了
确实不会有任何反应。这个代码只是改写了 confirm() 函数,只有在网页调用 confirm() 函数时才有效果。比如没有改写之前 confirm("确认保存?") 会弹出一个对话框,而改写后这个对话框就不会弹出来了,直接会收到【确定】的结果。
本来对弹出的对话框确认后会执行后面的语句,如果改写后直接会收到【确定】的结果,但confirm()后面的{document.frm.action="….”; document.frm.submit();} 也执行不了,就不是没有意义了吗?
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 10:28

tmplinshi wrote:你顶楼的代码还要加上等待“来自网页的消息”窗口出现才能成功。

32772 是 HSHELL_RUDEAPPACTIVATED 事件,在窗口激活时产生。最好是用另外一个消息 HSHELL_WINDOWCREATED,从名字可知道是窗口创建时产生。

Code: Select all

#NoEnv
#Persistent
SetBatchLines, -1

DllCall("RegisterShellHookWindow", "ptr", A_ScriptHwnd)
msgNum := DllCall("RegisterWindowMessage", "str", "SHELLHOOK")
OnMessage(msgNum, "ShellProc")
return

ShellProc(wParam, lParam) {
	if (wParam = 1) { ; HSHELL_WINDOWCREATED = 1
		WinGetClass, className, ahk_id %lParam%
		if (className = "Alternate Modal Top Most") {
			WinWait, 来自网页的消息
			ControlSend,, {Enter}
		}
	}
}
个人认为这个方法没有 WinEventHook 的方法好,毕竟这是监测窗口创建,而 WinEventHook 可以只监测 EVENT_SYSTEM_ALERT 消息。
图片可以上传到 http://upload.otar.im/
如果我把上面的代码和

Code: Select all

F2::
	Pwb := WBGet()
	Pwb.document.getElementById("saveButton").click()
return
放在一个脚本,就不能自动确认弹出对话框,分别运行就可以了,有办法能合在一起吗?
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 对弹出的对话框自动确认,却一直无法成功,请求tmplinshi及各位大侠指教

23 Sep 2016, 10:33

gongnl wrote: 本来对弹出的对话框确认后会执行后面的语句,如果改写后直接会收到【确定】的结果,但confirm()后面的{document.frm.action="….”; document.frm.submit();} 也执行不了,就不是没有意义了吗?
我测试是没有问题的。测试代码:

Code: Select all

Gui, Font, s15, 微软雅黑
Gui, Add, ActiveX, w600 h300 vwb, Shell.Explorer
Gui, Add, Button, w200, 测试
Gui, Show

wb.Navigate("about:tabs")
wb.document.write( html() )
Return

Button测试:
	wb.document.parentWindow.execScript("function confirm(){return true}")
	wb.document.getElementById("saveButton").click()
return

GuiClose:
ExitApp

html() {
	html =
	(
		<html>
			<script>
				function save() {
					if(confirm("确认保存?")){
						document.getElementById('result').innerText = "成功";
					}
				}
			</script>
			
			<input type=button id="saveButton" onclick="save()" value="保存" />
			<p>结果: <span id=result></span>
		</html>
	)
	return html
}

Return to “请求帮助”

Who is online

Users browsing this forum: No registered users and 13 guests