Subject: | click and form fill work differently (or fail) in Mechanize::Chrome vs Firefox |
Date: | Sat, 24 Nov 2018 11:09:08 -0500 |
To: | bug-WWW-Mechanize-Chrome [...] rt.cpan.org |
From: | Jeff Stalzer <ei69jeff [...] comcast.net> |
I was upgrading from Mechanize::Firefox to Mechanize::Chrome. Logging in
to a Bank web site w/ 2 button clicks and 2 form fields to fill in. It
works in M::Firefox but require changes or in the case of form fill in,
fail all together in M::Chrome.
I'm using Firefox 48.0.2 on Windows 7 and Strawberry Perl. Firefox must
be running before executing the sample code, Chrome will start by
itself. The log in fails because the credentials are phony.
The Perl code:
##test_fox_chrome.pl
#test log in page in mechanize::firefox and mechanize::chrome
use Win32;
use Date::Calc qw( Date_to_Text_Long Today_and_Now Date_to_Text);
use Win32::GUI();
use HTML::Parser;
#use HTML::FormatText;
use HTML::TreeBuilder;
use LWP 5.64; # Loads all important LWP classes, and makes
# sure your version is reasonably recent.
print "\n*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*";
print "\n<<< test_fox_chrome.pl >>>";
print "\n*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*";
print "\n\n*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*";
print "\n<<< load chrome >>>";
print "\n*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*";
use WWW::Mechanize::Chrome;
use Log::Log4perl qw(:easy);
print "\n\n*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*";
print "\n<<< load firefox >>>";
print "\n*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*";
use WWW::Mechanize::Firefox;
Log::Log4perl->easy_init($ERROR); # Set priority of root logger to ERROR
$url_read = "https://www.fnb-online.com/";
#mechanize::firefox version
eval {
$mech_fox = WWW::Mechanize::Firefox->new( ###need changes?
activate => 1, # bring the tab to the foreground
stack_depth => 5,
#autodie => 0, # make HTTP errors non-fatal
#host => 'localhost',
);
};
warn $@ if $@;
$trap_code = "";
$trap_code = $@ if $@;
MsgBox("Test Debug", "from WWW::Mechanize::Firefox->new " ##,
"Return code:" . $mech_fox->status()
. "|\nTrap code: |" . $trap_code . "|"
, 64) if $@; ;
eval {
$mech_fox->get($url_read );
};
warn $@ if $@;
$trap_code = "";
$trap_code = $@ if $@;
MsgBox(" Trap Error "," This is Mechanize::Firefox get"
. "|\nTrap code: |" . $trap_code . "|"
, 48) if $@;
eval {
$mech_fox->click_button( id => 'logintext' ); #'btnLogin' , "id"
};
warn $@ if $@;
$trap_code = "";
$trap_code = $@ if $@;
MsgBox(" Trap Error "," This is Mechanize::Firefox get"
. "|\nTrap code: |" . $trap_code . "|"
, 48) if $@;
$mech_fox->form_with_fields(
'txtUsername'
);
$mech_fox->set_fields(
'txtUsername' => "MYuserID" ,
);
$mech_fox->set_fields(
'txtPassword' => "MYpassword123" ,
);
$mech_fox->click_button( name =>
'ctl00$Body$splash_0$LoginHeader$btnSubmit' );
print "\n\n done loading page in firefox ";
MsgBox("Status" , "Did page load in firefox??", 48);
#mechanize::chrome version
eval {
$mech_chr = WWW::Mechanize::Chrome->new( ###need changes?
#activate => 1, # bring the tab to the foreground
#stack_depth => 5,
#autodie => 0, # make HTTP errors non-fatal
#host => 'localhost',
port => 9222, #port to connect to
#tab => 'New Tab',
launch_arg => [
##'--start-maximized',
'--disable-extensions',
##'--window-size=1000x800',
'--ignore-certificate-errors',
'--disable-plugins',
# '--disable-web-security',
# '--allow-running-insecure-content',
# '--load-extension',
# '--no-sandbox'
],
);
};
warn $@ if $@;
$trap_code = "";
$trap_code = $@ if $@;
MsgBox("Test Debug from WWW::Mechanize::Chrome->new ", "Return
code:" . $mech_chr->status()
. "|\nTrap code: |" . $trap_code . "|"
, 64) if $@; ;
eval {
$mech_chr->get($url_read );
};
warn $@ if $@;
$trap_code = "";
$trap_code = $@ if $@;
MsgBox(" Trap Error "," This is Mechanize::Chrome get"
. "|\nTrap code: |" . $trap_code . "|"
, 48) if $@;
eval {
##$mech_chr->click_button( id => 'logintext' ); #does NOT work, worked
in M::Firefox
$mech_chr->click( {text => "Log in to Personal Online Banking"} );
#WORKS!!!!
};
warn $@ if $@;
$trap_code = "";
$trap_code = $@ if $@;
MsgBox(" Trap Error "," This is Mechanize::Chrome click_button"
. "|\nTrap code: |" . $trap_code . "|"
, 48) if $@;
MsgBox("Kluge" , "Delay to see pop up open before next cmd kills
Chrome", 48);
eval {
$mech_chr->form_with_fields( 'txtUsername' );
};
warn $@ if $@;
$trap_code = "";
$trap_code = $@ if $@;
MsgBox(" Trap Error "," This is Mechanize::Chrome form_with_fields"
. "|\nTrap code: |" . $trap_code . "|"
, 48) if $@;
eval {
$mech_chr->set_fields( 'txtUsername' => "MYuserID" );
$mech_chr->set_fields( 'txtPassword' => "MYpassword123" );
};
warn $@ if $@;
$trap_code = "";
$trap_code = $@ if $@;
MsgBox(" Trap Error "," This is Mechanize::Chrome set_fields"
. "|\nTrap code: |" . $trap_code . "|"
, 48) if $@;
##$mech_chr->click_button( name =>
'ctl00$Body$splash_0$LoginHeader$btnSubmit' ); #goes to the wrong place
$mech_chr->click({ xpath => '//input[@type="submit"]' }); #WORKS!!!
print "\n\n done loading page in Chrome ";
MsgBox("Status" , "Did page load in Mechanize::Chrome??", 48);
exit -1;
sub MsgBox { #sub_MsgBox
#0 = OK
#1 = OK and Cancel
#2 = Abort, Retry, and Ignore
#3 = Yes, No and Cancel
#4 = Yes and No
#5 = Retry and Cancel
# 0 -------NO symbol-------
# 16 MB_ICONSTOP "X" in a red circle
# 32 MB_ICONQUESTION question mark in a bubble
# 48 MB_ICONEXCLAMATION exclamation mark in a yellow triangle
# 64 MB_ICONINFORMATION "i" in a bubble
#0 Error 1 OK 2 Cancel 3 Abort 4 Retry 5 Ignore 6
Yes 7 No !returns txt string NOT #
my ($caption, $message, $icon_buttons) = @_;
#check for writ log
if ( $gbl_log_fi_flg ) {
($year,$month,$day, $hour,$min,$sec) = Today_and_Now();
$t_tag = Right( "0" . $hour,2) . Right( "0" . $min,2) . Right(
"0" . $sec,2);
##convert "\n" to "\t" in $message or add "\t\t" after "\n|
after some number of nl
print $gbl_log_fi_fh $t_tag . "\t" . $caption . "\t" . $message
. $lin_trm;
}
my @return = qw/- Ok Cancel Abort Retry Ignore Yes No/;
my $result = Win32::MsgBox($message, $icon_buttons, $caption);
return $return[$result];
}
============================end========================
The screen log from running w/ the trappec failure:
F:\scrape_chrm_test\bug code>cd F:\Strawberry
F:\Strawberry>print "*testing C:\scrape_chrm_test\bug
code\test_fox_chrome.pl";
Unable to initialize device PRN
F:\Strawberry>perl "F:\scrape_chrm_test\bug code\test_fox_chrome.pl"
*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
<<< test_fox_chrome.pl >>>
*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*
*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
<<< load chrome >>>
*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*
*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
<<< load firefox >>>
*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*
done loading page in firefox No elements found for input with name
'txtUserna
me' at F:\scrape_chrm_test\bug code\test_fox_chrome.pl line 174.
done loading page in Chrome
F:\Strawberry>rem "what happened testing Chrome? -*-*-*-*-*-*-*-*"
F:\Strawberry>pause
Press any key to continue . . .