eval_in_page javascript执行在firefox中有效,但在phantomjs中无效

时间:2014-11-09 20:06:41

标签: javascript html perl phantomjs mechanize

我尝试使用Mechanize::PhantomJS在Perl中自动化一个javascript繁重的页面。在用户点击确认或在确认警报中取消后,页面上会执行一些javascript。由于我不知道如何按好,我直接执行javascript。 问题是以下脚本在使用firefox时工作正常(此处我使用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 ])在确认对话框中单击“确定”,但这不起作用。所以,我只是按照好的点击发出命令。

1 个答案:

答案 0 :(得分:2)

你误读了documentation

  

<强> $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 );