2016-07-12 13 views
-1

Ich möchte automatisch eine Form einer Website senden, aber mein Code nicht ausgeführt, können Sie erklären, warum? Wie kann ich das beheben? Hier ist der HTML-Code, der Bahn:Warum Code funktioniert nicht (ein Formular mit 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> 

Ich möchte ein Formular abzuschicken casperjs verwenden, hier ist mein Code:

// 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(); 

ich meinen Code laufen, aber es keine Sache erschienen nach dem Einreichen des Formulars der Titel der Seite sein. Vielen Dank!

Antwort

2

Zuerst im Code:

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

form[name ="login" sollte form[name ="login"] sein.

Zweitens sollten Sie select[name = <name>] verwenden, um ein <select> Element zu finden. Versuchen Sie diesen Code:

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); 

Mein Test Beispiel:

CasperJS Script:

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(); 

form.html:

<!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> 

-Test:

$ casperjs form.js 
Target Page 

Wenn Sie noch einige Probleme bekommen, können Sie hier einen Kommentar hinterlassen. Bitte denken Sie auch daran, meine Antwort zu akzeptieren, wenn alles gut funktioniert.