2

次のコードを使用してサイトにログインしようとしています

my $mech = WWW::Mechanize->new(autosave=>1);
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->follow_link( text => 'Sign In');
$mech->click();
$mech->field(UserName => "$username");
$mech->field(Password => "$password");
$mech->submit();

しかし、follow_link の間、href には 2 つのフロント スラッシュが含まれます (例: ( //test/sso-login))。したがって、follow_link はそれを URL 全体と見なし、以下のように失敗します。

Error GETing http://test/sso-login: Can't connect to test:80 (Bad hostname)

href は私が管理しているため、変更できません。この問題を克服し、この href を追加した完全な URL を取得する方法はありますか?

4

1 に答える 1

4

もちろん。を呼び出す直前に、Mech が見ている HTML を変更できますfollow_link()

my $html = $mech->content;
$html =~ s[//test/sso-login][http://example.com/test/sso-login]isg;
$mech->update_html( $html );

詳細については、ドキュメントを参照してください。そのページで「update_html」を検索します。

于 2012-08-11T03:56:03.543 に答える