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

All I want to do is click a login button to have the underlying JavaScript trigger and build the new web page. I just recently came back to Perl after, I don't know, 20 years? Anyway I got hung up on this little item for the last few days and would greatly appreciate some help. The code opens the website page in Firefox, and the button never gets clicked.
#!/usr/local/perls/perl-5.20.0/bin/perl use v5.10; use strict; use warnings; use diagnostics; use WWW::Mechanize::Firefox; use HTTP::Cookies; my $FEXdms = 'http://www.financeexpress.com/'; my $web_page = 'current'; my $mech = WWW::Mechanize::Firefox->new( tab => $web_page, activate => 1, # bring the tab to the foreground autoclose => 0 # to prevent autoclosing of the Tab ); $mech->get( $FEXdms ); $mech->click_button( name=> "btnLogin" );

---------------- results -----------------------------

Uncaught exception from user code: No elements found for //*(translate(local-name(.), "ABCDEFGHIJKLMNOPQRSTUVWX YZ", "abcdefghijklmnopqrstuvwxyz") = "button" and @name="btnLogin") or (translate(loc al-name(.), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="input" and @ type="button" or @type="submit" and @name="btnLogin") at test.pl line 20.

WWW::Mechanize::Firefox::signal_condition(WWW::Mechanize::Firefox=HASH(0x2644 b94), "No elements found for //*[(translate(local-name(.), \"ABCDEFGH"...) called at C:/Perl/site/lib/WWW/Mechanize/Firefox.pm line 2640

WWW::Mechanize::Firefox::xpath(WWW::Mechanize::Firefox=HASH(0x2644b94), "//*[ (translate(local-name(.), \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\", \""..., "node", undef, "sin gle", 1, "user_message", "Button name 'btnLogin' unknown") called at C:/Perl/site/lib /WWW/Mechanize/Firefox.pm line 3065

WWW::Mechanize::Firefox::click_button(WWW::Mechanize::Firefox=HASH(0x2644b94) , "name", "btnLogin") called at test.pl line 20

---------------------------------------------------

Replies are listed 'Best First'.
Re: can't click a simple button
by chacham (Prior) on Mar 18, 2015 at 21:29 UTC

    First things first. Make sure the button appears. Dump the page retrieved by $mech and verify it got the same page. Second, verify the item is found by Mechanize, such as with $mech->clickables() Once you know its there, you can concentrate on trying to click it. :)

      Eureka! While traversing the RE: threads on this (wonderful) site by the Monks, I found out how to derive the xpath using firebug. And bingo, it worked! Thanks for the help! Perl is fantastic. I'm programming again after 15 years layoff.

        Never mind. All previous login problems are solved. Just need to add some delay timers to get the code to work.