2016-07-12 1 views
-1

ウェブサイトのフォームを自動的に提出したいが、コードが実行されなかった。 これをどのように修正できますか? ここではウェブのhtmlコードです:コードが機能しない(casperjsを使用してフォームを送信する)理由

<form action="maill.php" method="GET" name="login"> 
    <input type="hidden" name="nav" value="" readonly="readonly" /> 
    <table> 
    <tr> 
     <td colspan=2><label for="seri">Seri</label></td> 
     <td colspan=2><input name="seri" type="number" value="" /></td> 
    </tr> 
    <tr> 
     <td colspan=2><label for="code">Code</label></td> 
     <td colspan=2><input name="code" type="number" value="" /></td> 
    </tr> 

    <tr> 
     <td colspan=2>type:</td> 
     <td><select name="type"> 
     <option value="...">...</option> 
     <option value="Viettel">Thẻ Viettel</option> 
     <option value="Mobiphone">Thẻ Mobiphone</option> 
     </select> 
    </td> 
    </tr> 

    <tr> 
    <td colspan=2>Value:</td> 
    <td><select name="value"> 
     <option value="...">...</option> 
     <option value="20">20.000 VNĐ</option> 
     <option value="50">50.000 VNĐ</option> 
    </select> 
    </td> 
</tr> 

<tr align="center"> 
    <td> 
    <input type="radio" name="server" value=1 /> Server 1 
    </td> 
    <td> 
    <input type="radio" name="server" value=2 /> Server 2 
    </td> 
</table> 
<button type="submit" value="Login" name="submit">Get gift</button><br /> 
</form> 

私はcasperjsを使用してフォームを送信したいが、ここで、私のコードです:

// initiate 
var casper = require('casper').create(); 

// submit form 
casper.start('http://shopchube.click/index3.html', function() { 
    this.fillSelectors('form[name ="login"', { 
    'input[name = seri ]' : '55883323777', 
    'input[name = code]' : '5591535443615', 
    'input[name = type ]' : 'Viettel', 
    'input[name = value ]' : '50', 
    'input[name = server ]' : '2', 
    }, true); 
}); 
// get title of the page after submit form 
casper.then(function(){ 
    this.echo(this.getTitle()); 
}); 

casper.run(); 

私は私のコードを実行しますが、それは任意のものを表示されませんでした投稿フォームの後のページのタイトルになります。ありがとうございました!

答えて

2

あなたのコードではまず、:

this.fillSelectors('form[name ="login"', {}, true); 

form[name ="login"form[name ="login"]でなければなりません。

第2に、<select>要素を見つけるには、select[name = <name>]を使用する必要があります。このコードを試してみてください。

this.fillSelectors('form[name ="login"]', { 
    'input[name = seri ]' : '55883323777', 
    'input[name = code]' : '5591535443615', 
    'select[name = type ]' : 'Viettel', 
    'select[name = value ]' : '50', 
    'input[name = server ]' : '2', 
}, true); 

私のテスト例:

CasperJSスクリプト: form.html

var casper = require('casper').create(); 
casper.start('http://localhost:63344/CasperSheet/form.html', function() { 
    this.fillSelectors('form[name ="login"]', { 
     'input[name = seri ]' : '55883323777', 
     'input[name = code]' : '5591535443615', 
     'select[name = type ]' : 'Viettel', 
     'select[name = value ]' : '50', 
     'input[name = server ]' : '2', 
    }, true); 
}).then(function(){ 
    this.echo(this.getTitle()); 
}); 

casper.run(); 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
<form action="target.html" method="GET" name="login"> 
    <input type="hidden" name="nav" value="" readonly="readonly" /> 
    <table> 
     <tr> 
      <td colspan=2><label for="seri">Seri</label></td> 
      <td colspan=2><input name="seri" type="number" value="" /></td> 
     </tr> 
     <tr> 
      <td colspan=2><label for="code">Code</label></td> 
      <td colspan=2><input name="code" type="number" value="" /></td> 
     </tr> 

     <tr> 
      <td colspan=2>type:</td> 
      <td><select name="type"> 
       <option value="...">...</option> 
       <option value="Viettel">Thẻ Viettel</option> 
       <option value="Mobiphone">Thẻ Mobiphone</option> 
      </select> 
      </td> 
     </tr> 

     <tr> 
      <td colspan=2>Value:</td> 
      <td><select name="value"> 
       <option value="...">...</option> 
       <option value="20">20.000 VNĐ</option> 
       <option value="50">50.000 VNĐ</option> 
      </select> 
      </td> 
     </tr> 

     <tr align="center"> 
      <td> 
       <input type="radio" name="server" value=1 /> Server 1 
      </td> 
      <td> 
       <input type="radio" name="server" value=2 /> Server 2 
      </td> 
    </table> 
    <button type="submit" value="Login" name="submit">Get gift</button><br /> 
</form> 
</body> 
</html> 

target.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Target Page</title> 
</head> 
<body> 
Target Page 
</body> 
</html> 

テスト:あなたはまだいくつかの問題を得た場合

$ casperjs form.js 
Target Page 

は、ここにコメントを残して自由に感じます。また、すべてがうまくいくなら、私の答えを受け入れることを覚えておいてください。

関連する問題