2017-01-04 2 views
0

でフォーム番号2が見つかりません要素「--disk-キャッシュを=真」:私が使用しているときphantomjs

my $form = $self->{obj_mech}->form_number(2); 

フォーム番号が見つかりません要素2/modules/TestLogin.pm 1129. /usr/local/share/perl/5.22.1/WWW/Mechanize/PhantomJS.pm line 796. WWW :: Mechanize :: PhantomJS :: signal_condition(WWW :: 5) Mechanize :: PhantomJS = HASH(0x4cfa120)、 /usr/local/share/perl/5.22.1/WWW/Mechanize/PhantomJS.pm行1732で呼び出された 「フォーム番号2の要素が見つかりません」)WWW :: Mechanize :: PhantomJS :: xpath(WWW :: Mechanize :: PhantomJS = HASH(0x4cfa120)、 "(//フォーム)[2]"、 "user_info"、 "フォーム番号2"、 "シングル" 、1) /usr/local/share/perl/5.22.1/WWW/Mechanize/PhantomJS.pm line 2102 WWW :: Mechanize :: PhantomJS :: form_number(WWW :: Mechanize :: PhantomJS = HASH 0x4cfa120)、 2)modules/TestLogin.pm行で呼び出されます。1129 collectLogin :: TestLogin_login(TestLogin = HASH(0x4f5c8a8))がcollectBets.pl行で呼び出されました。20デバッグ済みのプログラムが終了しました。 qを使用して またはRを終了して再起動するには、inhibit_exitを使用して、プログラム の終了、h q、h Rまたはh oの後で停止しないようにしてください。

ディスクカセットなしで正常に動作しています。 これはわかりやすいサンプルコードです。

#!/usr/bin/perl 
use strict; 
use warnings; 
use Helper; 
use WWW::Mechanize::PhantomJS; 
use DataBase; 
use MyConfig; 
use JSON; 
use DateTime; 
use HTML::Entities; 

sub new($$) { 
    my ($class,$params) = @_; 
    my $self = $params || {}; 
    bless $self, $class; 

    $self->{obj_mech} = WWW::Mechanize::PhantomJS -> new(phantomjs_arg => ['--ssl-protocol=any','--disk-cache=true','--max-disk-cache-size=1024'], ignore_ssl_errors => 1); 
    $self->{obj_helper} = new Helper(); 
    #$self->{obj_db} = new DataBase(); 
    $self->{logged_in} = 0; 
    #$self->setTorProxy(); 
    #$self->init_market_master(); 
    return $self; 
} 

Login(); 
print "\nlogin done...\n"; 
exit; 

sub Login { 
    my ($self) = @_; 
    my $html = $self->{obj_mech}->get("https://www.gmail.com/"); 
    sleep(25); 
    $html = $self->{obj_mech}->content; 
    $self->{obj_mech}->viewport_size({ width => 1366, height => 768 }); 

    my $form = $self->{obj_mech}->form_number(2); 

    my $user_name = '*****'; 
    my $password = '******'; 

    $self->{obj_mech}->set_fields('InputEmail' =>$user_name); 

    $self->{obj_mech}->set_fields('InputPassword' =>$password); 

    $self->{obj_mech}->click({ xpath => '//button[@class="PrimaryButton"]' }); 
    sleep(20); 
    my $test_html=$self->{obj_mech}->content; 
    $self->{obj_helper}->writeFileNew("TestLoginPage.html" , $test_html); 
    my $png = $self->{obj_mech}->content_as_png(); 
    $self->{obj_helper}->writeFileNew("LoginPage.png" , $png); 
    return 1; 
} 

答えて

0

まあ、ディスク・キャッシュの引数を見る前に、私はそのような要素がないことがわかりました。

# There is only 1 form. If you want to keep this line, 
# you need to change the form number to 1 
my $form = $self->{obj_mech}->form_number(2); 

# I didn't find input field named 'InputEmail' 
# The actual field name is 'Email' 
$self->{obj_mech}->set_fields('InputEmail' =>$user_name); 

# You have to click 'Next' button firstly then the password 
# input box is shown. And the field name should be 'Passwd' 
$self->{obj_mech}->set_fields('InputPassword' =>$password); 

# The xpath of 'Sign in' button is //input[@value="Sign in"] 
$self->{obj_mech}->click({ xpath => '//button[@class="PrimaryButton"]' }); 

ディスクキャッシュを持つか、ディスクキャッシュがあってもなくても、簡単な作業スクリプト:

#! /usr/bin/perl 

use strict; 
use warnings; 
use WWW::Mechanize::PhantomJS; 
use open ':std', ':encoding(UTF-8)'; 

#my $p = WWW::Mechanize::PhantomJS->new(phantomjs_arg=>['--ssl-protocol=any','--disk-cache=false','--max-disk-cache-size=1024'],ignore_ssl_errors=>1); 
my $p = WWW::Mechanize::PhantomJS->new(phantomjs_arg=>['--ssl-protocol=any','--disk-cache=true','--max-disk-cache-size=1024'],ignore_ssl_errors=>1); 

my $html = $p->get("https://www.gmail.com/"); 
sleep(5); 
write_html('first-page.html', $p->content); 

$p->viewport_size({width=>1366,height=>768}); 
my $form = $p->form_number(1); 

my $user_name = '*****'; 
my $password = '*****'; 

$p->set_fields('Email'=>$user_name); 
sleep(5); 

$p->click({xpath=>'//input[@value="Next"]'}); 
sleep(5); 
write_html('after-click-next.html', $p->content); 

$p->set_fields('Passwd'=>$password); 
sleep(5); 

$p->click({xpath=>'//input[@value="Sign in"]'}); 
sleep(5); 
write_html('after-login.html', $p->content); 

sub write_html { 
    my ($file, $content) = @_; 
    open my $fh, '>', $file or die; 
    print $fh $content; 
    close $fh; 
} 
関連する問題