in reply to Re^2: (OT) How to override the javascript alert when submitting a form using WWW::Mechanize::Firefox
in thread (OT) How to override the javascript alert when submitting a form using WWW::Mechanize::Firefox

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 });
  • Comment on Re^3: (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^4: (OT) How to override the javascript alert when submitting a form using WWW::Mechanize::Firefox
by jbernest (Novice) on May 03, 2013 at 08:41 UTC

    Corion, my script also gives me a javascript alert.

    My larger script uploads many data files. Some data trigger the alert and others don't, but I have no way of predicting which data will trigger the alert. The alert is not a problem, other than the fact that I need to get past it.

    When the alert occurs, I can use $mech->back to escape it. However, I don't know of a way to detect when it occurs.

    "$mech->is_visible()" in conditional statements don't work, as you can see if you run the script--the alert takes over the whole screen, but "form_with_fields('speciesList')" is returned as visible. Neither "$mech->submit()" nor "$mech->submit_form()" work for me on this webpage--the form just gets cleared and the page reloads without submitting.

    Any ideas? I would like to either disable the javascript alert (but not all javascript) at this step, or detect when it is triggered and use "$mech->back" to escape it.

    #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(autoclose => 0); $mech->get('http://david.abcc.ncifcrf.gov/summary.jsp'); $mech->click({ xpath => ('//*[@href="Upload a Gene List or Population" +]', single => 1), synchronize => 0 }); $mech->form_name('frmManager'); $mech->set_fields( 'pasteBox' => "RRI1, YLR149C, FUN19, YBR285W, ALD3, CUE5, +RTC3, AIM3, YDL199C, FMP45, AST2, GIP2, YKL091C, YKL133C, GLG2, SYM1, VHS3, YJR124C, MCH1, MHO +1, IKS1, RKM1, STF2, SAC1, ATG7, YAK1", 'Identifier' => "OFFICIAL_GENE_SYMBOL", ); $mech->click ({ xpath => ('//input[@name="rbUploadType" and @value="li +st"]', single => 1), synchronize => 0 }); $mech->click({xpath => ('//*[@value="Submit List"]', single => 1), syn +chronize => 0}); sleep 2; if ($mech->is_visible($mech->form_with_fields('speciesList'))) { print "It's visible.\n"; }

      There is no convenient way to detect window.alert() from WWW::Mechanize::Firefox. You can assign a dummy function to window.alert to override it. This will prevent alert boxes popping up until the next page load:

      my $window= $mech->repl->expr('window'); $window->{alert}= sub {}; ...

      Alternatively, you can put the override into Javascript directly:

      $mech->eval_in_page(<<'JS'); window.alert= function() {}; JS