Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

(OT) How to override the javascript alert when submitting a form using WWW::Mechanize::Firefox

by ajose (Acolyte)
on Aug 31, 2011 at 16:06 UTC ( #923456=perlquestion: print w/replies, xml ) Need Help??
ajose has asked for the wisdom of the Perl Monks concerning the following question:


I am trying to automate a form submission using WWW::Mechanize::Firefox. Populating the form works just fine as expected. But on submitting the form with the function $mech->click_button(), a javascript alert box gets opened. Without preventing the alert box opening, form submission does not happen.

Please find the HTML which opens the confirm dialog box.

<form onsubmit="return onSubmitProfile(this,'Please complete the red e +ntry.','Warning: Incorrect parameters may cause undesired behavior.') +" method="post" action="/goform/formHandlerConfigureProfileSettings"> <table width="100%" bgcolor="#ffffff" align="middle" valign="top"> </table> <fieldset> </fieldset> <fieldset id="content" style="display: none;"> </fieldset> <table > <tbody> <tr cellpadding="6" cellspacing="6" width="100%"> <td width="5%"> </td> <td width="45%"> </td> <td width="50%"> <input type="submit" value="Apply" name="button"> </td> </tr> </tbody> </table> </form>

The javascript function for the same is

function onSubmitProfile(theform,errorMessage,confirmMessage) { if(validateIpSettings() != 0) {alert(errorMessage); return false} var submitConfirm=confirm(confirmMessage) if(submitConfirm==true) { return true } return false }

As a work around for this issue, I used $mech->submit to submit the form by avoiding javascript. But this also avoid the error checking withing the web page. So can anyone help me here to trap the alert message which is displayed during the form submission. Also I tried to trap the alert with the function $mech->eval_in_page. But I could not find any detailed examples to use this function for the same purpose.

Any suggestions in this issue is appreciated.

  • Comment on (OT) How to override the javascript alert when submitting a form using WWW::Mechanize::Firefox
  • Select or Download Code

Replies are listed 'Best First'.
Re: (OT) How to override the javascript alert when submitting a form using WWW::Mechanize::Firefox
by Corion (Pope) on Aug 31, 2011 at 16:19 UTC

    So, what Perl code did you write, and how did it fail?

    How about you do the manual validation by calling validateIpSettings()?

    How about you have your program fill in only correct values and skip the Javascript validation?

      Thanks Corion. Validating the field values outside the form is not an issue. As I mentioned earlier, right now I am doing the form submission skipping the javascript validation. But the webpage under test which I am trying to automate, sometimes give invalid alert messages based on the values in certain fields. These messages are generated by the javascript. So if I ignore the javascript validation in the webpage, form will be submitted successfully but the error with the javascript goes unnoticed.

      For example, if I submit the form manually by selecting the 'OK' button, the webpage gives an alert message which says "Please complete the red entry" even when there are no empty fields. And then webpage submission does not get initiated. This is the main reason for which I cannot ignore the alert boxes in the webpage.

      Now I believe that I made my point clear to you. If it does, that justifies this lengthy reply. Please help me to sort out this issue.

        You still don't show any Perl code, so I can't comment on how you call your Javascript and how it fails for you.

        Personally, I check Javascript validation functions without actually submitting the form:

        # Set up values to be tested ... $mech->field( $name => $value ); ... my ($value, $type) = $mech->eval_in_page('validateIpSettings()'); is($value, 'some expected value');

        As your Javascript already is structured nicely, this conveniently eliminates the need for overriding window.alert. If you still want to override window.alert, you can do so by passing in the appropriate hash in the $env parameter to ->eval_in_page():

        # create myalert as Javascript function my $myalert = $mech->declare(<<JS); function (msg) {}; JS my ($value, $type) = $mech->eval_in_page('validateIpSettings()', { + alert => $myalert });

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://923456]
Approved by Old_Gray_Bear
and the shadows deepen...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2018-07-23 02:37 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (459 votes). Check out past polls.