我正在尝试使用Perl在Perl中自动化javascript重磅页面Mechanize::PhantomJS
。用户单击“确定”或“取消”确认警报后,页面上将执行一些javascript。由于我不知道如何按OK,所以我直接执行javascript。该问题是,下面的脚本使用火狐(这里我用的时候正常工作Mechanize::Firefox
),但不会产生任何结果时使用Mechanize::PhantomJS
$mech->eval_in_page(<<'JS');
closeChildWindows();
commandInProgress = true;
document.dataForm.target="_self";
document.dataForm.method='post';
document.dataForm.action="ReviewApptAction";
document.dataForm.submit();
JS
在PhantomJS中,脚本通过这些行而不会产生任何错误,但是不会在页面上执行任何操作,这意味着我没有得到任何结果,例如最后提交表单。有人知道这是怎么回事吗?
我想使用Mechanize::PhantomJS
它,因为它使我可以同时运行脚本的多个实例,这与firefox不同。
让我更清楚地说明这一点:我必须按下一个带有onclick javascript的按钮:
<a href="javascript:bookAppointment()" onmouseover="window.status='Next Screen';return true" onmouseout="window.status='';return true">
<img src="../images/include/buttonnext.gif" width="61" height="16" border="0" alt="Next Screen"></a>
该按钮调用的功能是这样的:
function bookAppointment()
{
if ( confirm("Book this appointment?") )
{
if ( !commandInProgress) {
closeChildWindows();
commandInProgress = true;
document.dataForm.target="_self";
document.dataForm.method='post';
document.dataForm.action="ReviewApptAction";
document.dataForm.submit();
}
else {
alert("Request has been submitted but not yet processed by the server. Please press OK and wait for response...");
}
}
return;
}
首先,我曾经$mech->confirm( 'Really do this?' [ => 1 ])
在“确认”对话框中单击“确定”,但这没有用。因此,我只是简单地发出了可以单击的命令。
您误读了文档:
$mech->confirm( 'Really do this?' [ => 1 ])
记录确认(默认为“ 1”或“确定”)[...]
正如Artjom B.在评论中所说,[ => 1 ]
表示第二个参数是可选的,默认值为1。如果要传递第二个参数,则必须删除方括号,因为以下内容是无效的Perl代码,并且将导致语法错误:
$mech->confirm( 'Really do this?' [ => 1 ]);
syntax error at ./foo line 42, near "'Really do this?' ["
Execution of ./foo aborted due to compilation errors.
要点击“确定”,请执行以下操作:
$mech->confirm( 'Really do this?' => 1);
或由于默认值为1,因此只需:
$mech->confirm( 'Really do this?' );
如果要取消对话框,请使用:
$mech->confirm( 'Really do this?' => 0 );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句