casperjs填写并点击不工作,因为我预期不能单击按钮

时间:2014-03-19 19:26:35

标签: html forms click phantomjs casperjs

好的,所以我将发布到我试图使用的html表单下面的代码集 并提交我正在尝试的casperjs代码。问题出现时,我试图点击提交按钮我拍摄截图并获得生成的HTML但由于某种原因html没有任何变化,屏幕截图显示填写的字段但表单尚未提交可以有人帮助我< / p>

继承人html

            <form action="userlogin.html" method="post" name="reg" id="reg" onsubmit="document.getElementById('sbt').disabled = true;return true;">
        <input type="hidden" name="submit" value="1" class="input">
          <table cellpadding="0" cellspacing="2">
            <tbody><tr>
              <td valign="top" width="123"><font face="Arial, Helvetica, sans-serif" size="2"><b>user</b></font></td>
              <td width="287"> <font face="Arial, Helvetica, sans-serif" size="2">
                <input type="text" name="login" size="20" maxlength="20" value="" class="input">
                <a href="/remember.html">forgot?</a></font> <font face="Arial, Helvetica, sans-serif" size="2">
                </font>
                <div class="error"><font face="Arial, Helvetica, sans-serif" size="2"></font></div>
              </td>
            </tr><tr>
            </tr><tr>
              <td valign="top" width="123"><font face="Arial, Helvetica, sans-serif" size="2"><b>Password:</b></font></td>
              <td width="287"> <font face="Arial, Helvetica, sans-serif" size="2">
                <input type="password" name="password" size="20" class="keyboardInput input" id="keyboardInputInitiator0"><img src="/img/keyboard.png" alt="Keyboard interface" class="keyboardInputInitiator" title="Display graphical keyboard interface">
                </font>
                <div class="error"><font face="Arial, Helvetica, sans-serif" size="2"></font></div>
              </td>
            </tr><tr>
            </tr><tr>
              <td valign="top" width="123" height="23"><font face="Arial, Helvetica, sans-serif" size="2">&nbsp;</font></td>
              <td width="287" height="23"><font face="Arial, Helvetica, sans-serif" size="2">&nbsp;</font>
              </td>
            </tr>
            <tr>
              <td valign="top" width="123"><font face="Arial, Helvetica, sans-serif" size="2"><b>Turing
                number:</b></font></td>
              <td width="287"><font face="Arial, Helvetica, sans-serif" size="2"><font face="Arial, Helvetica, sans-serif" size="2">
                <input type="text" name="turing" size="7" maxlength="10" class="input">
                </font>
                <div class="error"><font face="Arial, Helvetica, sans-serif" size="2"></font></div></font></td>
            </tr>
            <tr>
              <td valign="top" width="123"><font face="Arial, Helvetica, sans-serif" size="2"><img id="cpt_img" src="/user/turing/image.asp?1395177754"><br>
<a style="text-decoration:none;cursor:pointer;" href="javascript:void(0);" onclick="document.images['cpt_img'].src='/user/turing/image.asp?'+(new Date()).getTime();return false;">
<table><tbody><tr><td><img src="/img/reload.gif"></td><td valign="middle"></td></tr></tbody></table></a></font></td>
              <td width="287">
              </td>
            </tr>
            <tr>
              <td valign="top" width="123" height="40">&nbsp;</td>
              <td width="287" height="40" valign="bottom">
                <input type="submit" value="Authorize" name="submit" id="sbt" class="input submit">
              </td>
            </tr>
          </tbody></table>

和我的casperjs

var casper = require('casper').create ({
waitTimeout: 15000,
stepTimeout: 15000,
verbose: true,
viewportSize: {
width: 1280,
height: 960
},
pageSettings: {
"userAgent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10',
"ignoreSslErrors": true
},
onWaitTimeout: function() {
  // casper.capture('./out/wait-timeout:_' + TimeTidy() + '.png');
  // throw new Error stuff;
},
onStepTimeout: function() {
  // casper.capture('./out/step-timeout' + TimeTidy() + '.png');
  // throw new Error stuff;
 }
});
var fs = require('fs');

var system = require('system');
pageSettings: {
        webSecurityEnabled: false;
}

casper.start('https://example/login.html', function() {

// Get Image Needed..
//var abc = this.getHTML('img#cpt_img', true).match(/src="(.*?)"/)[1];
//var url = 'https://example' + abc;
//this.download(url, "abc.jpg");

// Get Some Data..
system.stdout.writeLine('Data Needed To Continue .');
//var line = system.stdin.readLine();
//console.log(line);

// Get HTML Source Code..
var el = this.getHTML();
fs.write('results.html', el, 'w');

casper.fill('form[name="reg"]', {
'login': '43532243',
'password': 'dsfgsfdgh',
'turing': '3456'
}, true);





casper.then(function() {
    this.capture("filename.jpg");
var el2 = this.getHTML();
fs.write('results2.html', el2, 'w');
});
});


casper.run();

为什么我的表单不能正确提交

2 个答案:

答案 0 :(得分:0)

尝试使用:

this.fillSelectors('form[id="signup_frm"]', { 
    "input[name='nick_name']": 'bobby', 
    "input[name='e_mail']": 'robert',
    "input[name='password']": password,
    "input[name='password2']": password,
    "input[name='gender']": 'M',
    "input[name='newsletter']": true
}, false);
this.click('.submit');

答案 1 :(得分:0)

您已经处于casper环境中。尝试使用this.fill方法。简单地用这个替换casper。见下面的例子。

this.fill(&#39;表格[name =&#34; reg&#34;]&#39;,{ &#39;登录&#39;:&#39; 43532243&#39;, &#39;密码&#39;:&#39; dsfgsfdgh&#39;, &#39; turing&#39;:&#39; 3456&#39; },true);