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

Why code doesn't work to (submit a form using casperjs)

本文关键字:提交 表单 casperjs 使用 代码 不起作用 为什么      更新时间:2023-09-26

我想自动提交一个网站表单,但我的代码没有运行,你能解释一下为什么吗?我该如何解决这个问题。这是网络的 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();

我运行了我的代码,但在提交表单后,页面标题似乎没有任何内容。谢谢!

首先,在你的代码中:

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

形式.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>

目标.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