为什么代码不起作用(使用casperjs提交表单)

时间:2016-07-12 17:53:55

标签: javascript phantomjs casperjs

我想自动提交一个网站表单,但我的代码没有运行,你能解释一下原因吗? 我怎样才能解决这个问题。 这是网络的HTML代码:

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

我运行我的代码但是在提交表单后它没有出现任何关于页面标题的东西。谢谢!

1 个答案:

答案 0 :(得分:2)

首先,在您的代码中:

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

form[name ="login"应为form[name ="login"]

其次,您应该使用select[name = <name>]来查找<select>元素。试试这段代码:

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脚本

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>

<强>测试

$ casperjs form.js
Target Page