casperjs fill and click not working as i expected

2019-09-08 17:06发布

问题:

okay so i will post to sets of code below one the html form i am trying to use and submit and my casperjs code i am trying. the problem comes when trying to click the submit button i take a screenshot and get the resulting html but for some reason the html has no change at all and the screenshot shows the fields filled in but the form has not been submitted can someone help me

heres the 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>

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

why will my form not submit properly

回答1:

Try using this :

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


回答2:

you are already in the casper context. try using this.fill method. simply replace casper with this. See example below.

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