为什么代码不起作用(使用 casperjs 提交表单)
Why code doesn't work to (submit a form using 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();
我运行了我的代码,但在提交表单后,页面标题似乎没有任何内容。谢谢!
首先,在你的代码中:
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
相关文章:
- Jquery提交表单而不刷新
- 如何在提交表单时将PHP变量传递到Javascript cookie中
- 每次提交表单时都会重新加载网页
- Razor中的自动提交表单
- 使用 JavaScript 自动提交表单
- 点击相同的按钮打开模型,然后提交表单
- 添加和删除隐藏字段数组中的值,而不提交表单
- 使用Ajax提交表单
- jQuery:使用“jQuery验证”后无法提交表单's远程验证
- 如何在不重新加载的情况下提交表单,并使用Node+Express将数据传递回同一页面
- 有登录表单时无法提交表单
- 带有select的jquery提交表单不起作用
- 如何防止回车键提交表单,但仍然允许回车工作
- 使用按钮和单选按钮提交表单,但始终使用相同的值
- 在提交表单之前确定重复值
- 如何创建动态ajax提交表单
- HTML提交表单,同时包含空字段检查和按钮隐藏
- 如何使用jQueryAjax使用动态html输入texbox提交表单
- 当触发下拉列表的onchange事件时,使用JavaScript提交表单
- 提交表单后,Watir文本框输入未持续(重置为以前的文本框输入)