【问题标题】:eval_in_page javascript execution works in firefox but not in phantomjseval_in_page javascript 执行在 firefox 中有效,但在 phantomjs 中无效
【发布时间】:2015-01-06 02:26:59
【问题描述】:

我正在尝试使用Mechanize::PhantomJS 在 Perl 中自动化一个 javascript 繁重的页面。用户在确认警报中单击确定或取消后,页面上会执行一些 javascript。由于我不知道如何按 OK,所以我直接执行 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-&gt;confirm( 'Really do this?' [ =&gt; 1 ]) 在确认对话框中单击“确定”,但这不起作用。所以,我只是发出了 OK 点击之后的命令。

【问题讨论】:

    标签: javascript html perl phantomjs mechanize


    【解决方案1】:

    你误读了documentation

    $mech-&gt;confirm( 'Really do this?' [ =&gt; 1 ])

    记录确认(默认为“1”或“ok”)[...]

    正如 Artjom B. 在 cmets 中所说,[ =&gt; 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 );
    

    【讨论】:

    • 终于有人遵守了 SO 礼仪(以 CW 身份发布)。我从来没有使用过 Perl,所以我不太愿意根据假设写出答案。
    • 好吧,你解决了问题,并且告诉 OP 写一个答案是正确的。我现在正在使用WWW::Mechanize::PhantomJS,所以我认为将答案放在一起而不是等待OP会更快。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多