Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

WWW::Mechanize::Firefox trigger onchange event

by vijayvithal (Novice)
on Aug 25, 2015 at 11:12 UTC ( [id://1139818]=perlquestion: print w/replies, xml ) Need Help??

vijayvithal has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to manipulate the form at this link

The code uses JS to populate the options for the next element based on the current element. While I have been able to select the element for district I am not able to trigger the script to load the next element. Any pointers on what I am doing wrong.

use strict; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); $mech->get('http://117.247.176.82/VIEWDOCS.aspx'); $mech->autoclose_tab( 0 ); $mech->allow( javascript => 1 ); my @el = $mech->by_id('MainContent_ddldist'); $el[0]->__event('mousedown'); $mech->select( 'ctl00$MainContent$ddldist'=>'BN'); $el[0]->__event('mouseup'); #Well the mouse event did not work, lets trigger on change directly. $el[0]->__event('onChange'); my @el=$mech->by_id('ctl01'); $el[0]->__event('click'); #even this did not work and I have spent over a day on this... time to + seek the wisdom of the monks?. sleep 10

Replies are listed 'Best First'.
Re: WWW::Mechanize::Firefox trigger onchange event
by anonymized user 468275 (Curate) on Aug 25, 2015 at 11:51 UTC
    Are you sure you are traversing the DOM carefully? How about, for example, testing the return value of all those calls, e.g.:
    use strict; use WWW::Mechanize::Firefox; use Data::Dumper; my $mech = WWW::Mechanize::Firefox->new(); $mech->get('http://117.247.176.82/VIEWDOCS.aspx'); $mech->autoclose_tab( 0 ); $mech->allow( javascript => 1 ); my @el = $mech->by_id('MainContent_ddldist'); @el or whoops('by_id','MainContent_ddldist',@el); $el[0]->__event('mousedown'); or whoops('mousedown', $el[0]); my $result = $mech->select( 'ctl00$MainContent$ddldist'=>'BN'); $result or whoops('select','ctl100$MainContent$ddldist', $result); # etc... sub whoops { printf STDERR ('Error during %s:%s', shift(), "\n"); @_ and print STDERR dumper(@_); exit 256; }
    Update: and if necessary also log the successful calls to STDOUT
    my $globalLogLevel = 1; # ... my @el = $mech->by_id('MainContent_ddldist'); @el ? log('by_id','MainContent_ddldist',@el) : whoops('by_id','MainContent_ddldist'); #... sub log { $globalLogLevel or return; printf ('Success with %s:%s', shift(), "\n"); @_ and print dumper(@_); }

    One world, one people

Re: WWW::Mechanize::Firefox trigger onchange event ( ->click )
by Anonymous Monk on Aug 25, 2015 at 11:23 UTC
    Try  $mech->click({ xpath => qq{//*[\@id="ctl01"]}  });
Re: WWW::Mechanize::Firefox trigger onchange event
by belg4mit (Prior) on Sep 19, 2016 at 13:07 UTC
    Just in case anyone else is looking to solve the same problem as I, this approach of using the private methods to click on elements worked for me when JavaScript didn't seem to be triggering as it ought. I could not click() either because Microsoft does not use proper HTML in the design of its UI; no actual form, and anchor tags as buttons. Hopefully that's enough keywords to help others out.

    --
    In Bob We Trust, All Others Bring Data.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1139818]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found