如何使用没有表单标签的CasperJS填写表单

时间:2013-10-18 09:32:40

标签: javascript casperjs html-input

我是CasperJS的新手,我在登录此网站时遇到问题http://weibo.com/login.php

这是我尝试过的事情

this.fill('form#contact-form', {
    'username':    'test@test.com',
    'password':    'anypassword',

}, true);

我不能用它,因为它没有形式。

所以我尝试使用sendKeys的另一种方法。

this.sendKeys('.W_input ', 'tinkerbell@gmail.com');

现在我的问题是输入文本中没有ID,只有CLASS,用户名和密码都有相同的CLASS。如何只使用该类输入该文本框?或者是否可以使用XPath使用sendKeys?

3 个答案:

答案 0 :(得分:8)

casper支持CSS3选择器(tuts+ has a decent rundown of the top 30 you should memorize),因此您可以执行以下操作:

this.sendKeys('input[name=username]', 'tinkerbell@gmail.com');

答案 1 :(得分:5)

我使用querySelector函数成功设置用户名和密码:

var casper = require("casper").create();
casper.start('http://www.weibo.com', 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();

答案 2 :(得分:-1)

这是填写登录表单的简单casper js脚本 假设它已经为每个字段提到了id

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@email.com', 'password');