route127の日記: WWW::Mechanize::Chrome(受信料支払い)
受信料の支払い期限が今日だったのでスクリプトを書いてた。
以前WWW::Mechanize::Firefoxで書いてたがいい加減古いFirefox続けるのもダサいし、いよいよNode.jsデビューか、とも思ったが、Win10にしたらWWW::Mechanize::Chromeがインストール通るようになったのでこれで書き直した。
実行してみるとmozreplの頃のようなもっさり感はない。
またブラウザの起動もFFと違ってsystem関数なんかで自前でやらずに済むので使いやすくなっている。
click({selector => ...})
はWWW::Mechanize::Firefoxにもあったけれどもクリックしたが最後、処理が戻ってこないみたいなこともあったりして、
click({selector => ..., synchronize => 0})
みたいに書き換えたりする必要があった。
WWW::Mechanize::Chromeになってwait_until_visibleなんかもできてその辺書き易くなった。
こんな感じでフォーム入力とクリックを開発者ツールでCSSセレクタをコピーしながら書き下していくだけ。
use strict;
use warnings;
use WWW::Mechanize::Chrome;
#(入力パラメータ省略)
my $url = 'https://pid.nhk.or.jp/jushinryo/PayCreditLumpMove.do';
my $mech = WWW::Mechanize::Chrome->new(autoclose => 0);
$mech->get($url);
#-----------
$mech->wait_until_visible(selector => 'body');
$mech->form_name('form1');
$mech->click({selector => '#includeMain > div > div.indent02 > div.btnArea.btnArea2 > div.agreeCheck > span > a'});
$mech->click({selector => '#includeMain > div > div.indent02 > div.btnArea.btnArea2 > a > img' });
#-----------
$mech->wait_until_visible(selector => 'body');
$mech->field('#customerNum1' => $num1);
$mech->field('#customerNum2' => $num2);
$mech->field('#customerNum3' => $num3);
$mech->field('#assist_cd' => $confirm);
$mech->click({selector => '#includeMain > div > div.indent02 > div.btnArea.btnArea3.mt10 > a > img'});
#-----------
$mech->wait_until_visible(selector => 'body');
$mech->field('#CreditcardNumber1', $card1);
$mech->field('#CreditcardNumber2', $card2);
$mech->field('#CreditcardNumber3', $card3);
$mech->field('#CreditcardNumber4', $card4);
$mech->select('Ie510CardExpiryM', $month);
$mech->select('Ie510CardExpiryY', $year);
$mech->field('#SecurityCord', $security);
$mech->field('#mailAddress', $mail);
$mech->field('#confirmMailAddress', $mail);
$mech->click({selector => '#includeMain > div > div.indent02 > div.btnArea.border-transparent.pl0.pb30 > a > img'});
他の機能、Log4perlとかJSON周りについてまだよく読んでないが、WWW::Mechanize::Firefoxで諦めてた事ができるようになるかもしれないと思ったら楽しくなってきた。
だがその前にこれまでのFF用スクリプトを移植していかないといけない。
WWW::Mechanize::Chrome(受信料支払い) More ログイン