3

WWW::Mechanizeperl モジュールを使用して post メソッドでフォームを送信しようとしています。

use WWW::Mechanize;

my $mech = WWW::Mechanize->new();
...
$mech->get($url);
...
my $response = $mech->submit_form(
        form_name => $name,
        fields    => {
                        $field_name => $field_value
                         },
        button    => 'Button'  
    );

$field_nameは一般的にテキスト フィールドであり (ただし、型はフォームで明示的に指定されていません)、事前設定された値があります。

$field_name => $field_value何らかの理由で$mech->submit_form値を置き換えず、代わり$field_valueに元の値の後にフォームに追加されます。

{submitted_field_value} = {original_value},{provided_value}

提出するフォームで{original_value}と置き換える方法は?{provided_value}

4

3 に答える 3

1

$mech->submit_form() を呼び出す前に、次の 1 行をコードに追加するとどうなりますか。

$mech->field( $name, [$field_value], 1 );

これにより、最初の値が追加されるか、既に存在する場合は上書きされます。 1は数値パラメーター (または位置インデックス) です。

WWW::Mechanize のドキュメントを参照してください。

$mech->field( $name, \@values, $number )

フィールドの名前を指定して、その値を指定された値に設定します。[...]

オプションの $number パラメータは、同じ名前の 2 つのフィールドを区別するために使用されます。フィールドには 1 から番号が付けられます。

于 2012-01-01T10:45:06.930 に答える
0

It's important to remember WWW::Mechanize is better thought of as a 'headless browser' as opposed to say LWP or curl, which only handle all the fiddly bits of http requests for you. Mech keeps its state as you do things.

You'll need to get the form by using $mech->forms or something similar (its best to decide from the documentation. I mean there so many ways to do it.), and then set the input field you want to change, using the field methods.

I guess the basic way to do this comes out as so:

$mech->form_name($name);
$mech->field($field_name, $field_value);
my $response = $mech->click('Button');

Should work. I believe it will also work if you get the field and directly use that (ie my $field = $mech->form_name($name); then use $field methods instead of $mech.

于 2012-01-01T01:38:49.973 に答える
0

私は自分の意志でそれを機能させることができました。あなたの提案をしてくれたTimbusとknbに感謝します。私のケースは完全に一般的ではないかもしれませんが(プリセット値を知っています)、私が見つけたものを共有します(トレイルとエラーによる)。

my $mech = WWW::Mechanize->new();
$mech->get($url);  
$mech->form_name( $name );

my $fields = $mech->form_name($name);
foreach my $k ( @{$fields->{inputs}}){
 if ($k->{value} eq $default_value){
   $k->{value}=$field_value;
   }
}

my $response = $mech->click('Button_name');
于 2012-01-03T23:21:08.557 に答える