最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to fill up form using CasperJS without form tag - Stack Overflow

programmeradmin4浏览0评论

I'm new to CasperJS and I'm having problems in logging in into this site .php

here's what I've tried

this.fill('form#contact-form', {
    'username':    '[email protected]',
    'password':    'anypassword',

}, true);

I can't used that since it has no form.

so I tried a different method using sendKeys.

this.sendKeys('.W_input ', '[email protected]');

Now my problem in here is that the input text has no ID in it, only a CLASS and both username and password have the same CLASS in it. how can i type into that textbox using only that class? or is it possible to use sendKeys using XPath?

I'm new to CasperJS and I'm having problems in logging in into this site http://weibo./login.php

here's what I've tried

this.fill('form#contact-form', {
    'username':    '[email protected]',
    'password':    'anypassword',

}, true);

I can't used that since it has no form.

so I tried a different method using sendKeys.

this.sendKeys('.W_input ', '[email protected]');

Now my problem in here is that the input text has no ID in it, only a CLASS and both username and password have the same CLASS in it. how can i type into that textbox using only that class? or is it possible to use sendKeys using XPath?

Share Improve this question edited Sep 23, 2015 at 21:10 Artjom B. 61.9k25 gold badges134 silver badges229 bronze badges asked Oct 18, 2013 at 9:32 Alex StraussAlex Strauss 1213 silver badges7 bronze badges 1
  • sendKeys use keyboard event to input. That may cause some weird problem... – Sayalic Commented Apr 30, 2016 at 6:48
Add a ment  | 

3 Answers 3

Reset to default 8

casper supports CSS3 selectors (tuts+ has a decent rundown of the top 30 you should memorize) so you could do something like:

this.sendKeys('input[name=username]', '[email protected]');

I use querySelector function to set username and password with success:

var casper = require("casper").create();
casper.start('http://www.weibo.', function() {
    this.evaluate(function(username, password) {
        document.querySelector('input[node-type="username"]').value = username;
        document.querySelector('input[node-type="password"]').value = password;
        document.querySelector('.W_btn_g:eq(1)').click();
    }, 'your_username', 'your_password');

});

casper.then(function() {
    this.wait(50000, function() {
        this.echo("50s");
    });

});

casper.then(function() {
    this.capture('weibo.png');
});

casper.run();

This is simple casper js script to fill login form assuming it has mentioned id's for each field

casper.evaluate(function(username, password) { document.querySelector('input#user_email').value = username;

      document.querySelector('input#user_password').value = password;
      document.querySelector('input#login-button').click();
  }, '[email protected]', 'password');
发布评论

评论列表(0)

  1. 暂无评论