这是我第一次与Casperjs合作。我的表单没有ID或名称,但需要提交用户名和密码。
在HTML正文中有一个onload =" init(document.forms [0]);"
表格:
<form method="post">
<table border="0" cellspacing="0" cellpadding="0">
<tbody><tr>
<script>
document.write('<td class="username">');
document.write(langstr[ui_CurrentLanguage].login_username);
document.write(' : ');
document.write('</span>');
document.write('<td colspan="2"><input type="text" class="inputname" id="user_name" name="user_name" onKeyPress="if(event.keyCode == \'13\') Validate();" size="20" maxlength="65" autocomplete="off"></td>');
</script><td class="username">USERNAME : </td><td colspan="2"><input type="text" class="inputname" id="user_name" name="user_name" onkeypress="if(event.keyCode == '13') Validate();" size="20" maxlength="65" autocomplete="off"></td>
</tr>
<tr>
<script>
document.write('<td class="password">');
document.write(langstr[ui_CurrentLanguage].login_password);
document.write(' : ');
document.write('</span>');
document.write('<td colspan="2"><input type="password" class="inputpwd" id="user_passwd" name="user_passwd" onKeyPress="if(event.keyCode == \'13\') Validate();" size="20" maxlength="64" autocomplete="off"></td>');
</script><td class="password">PASSWORD : </td><td colspan="2"><input type="password" class="inputpwd" id="user_passwd" name="user_passwd" onkeypress="if(event.keyCode == '13') Validate();" size="20" maxlength="64" autocomplete="off"></td>
</tr>
</tbody></table>
我在引用表单时遇到问题,因此我可以在casperjs中使用fill方法。关于如何获得对此表单的引用的任何想法?
感谢。
答案 0 :(得分:1)
您需要使用sendKeys()填写表单并发送密钥“13”(回车):
var casper = require('casper').create(), utils = require('utils');
casper
.start('http://',function(){
this.sendKeys('#user_name', 'your_name', {keepFocus: true});
this.sendKeys('#user_passwd','your_pass', {keepFocus: true});
this.sendKeys('#user_passwd', casper.page.event.key.Enter , {keepFocus: true});
this.wait(3000,function(){
this.capture('test.png');
utils.dump(this.getTitle());
});
})
.run();
另请参阅:this issue