16

Following is the hlml of the login form that I have

<div class="login_area_user">

            <form method="post" action="https://www.tradus.com/login?dest_url=https://www.tradus.com/cart/select-address" id="user-login">
            <input type="hidden" value="1" name="form_submit">

                <h3 style="display:inline-block;">Already a Member</h3>

                <p id="login-main-center-right-descp">You can use tradus login id and password</p>

                <div class="login-row">
                    <label class="colorBlack">Email / Login*</label>
                    <input class="login-field" type="text" name="name" id="edit-namepopup">
                </div> <!-- [/login-row] -->

                <div class="login-row">
                    <label>Password</label>
                    <input class="login-field" type="password" id="edit-passpopup" name="pass">
                </div> <!-- [/login-row] -->

                <div class="login-row">            
                    <a class="forgotPassword" href="/forgot_password">Forgot your password?</a>
                    <!--input type="checkbox" name="remember" /><span>Remember me</span-->
                </div>

                <div class="login-row">
                    <input class="login-button" value="Login" type="submit">
                </div>

                <input type="hidden" name="op" value="Log in">

            </form>

        </div>

Am using the following code to login :

this.fill('form#user-login', {
        'form_submit':    1,
        'name':    'abc@gmail.com',
        'pass':   'pwd',
        'op':       'Log in'

    }, true);

But I dont thing its doing the thing for me.

4

4 回答 4

19
casper.waitForSelector("form input[name='name']", function() {
    this.fillSelectors('form#user-login', {
        'input[name = name ]' : 'abc@gmail.com',
        'input[name = pass ]' : 'pwd'
    }, true);
});

只需使用它(请参阅waitForSelector 文档)。首先,等待表单被加载。然后使用选择器填写表格。

于 2014-02-05T11:59:11.980 回答
10
casper.waitForSelector('form', function(){
    this.fill('form', {
    'name': 'abc@gmail.com', 
    'pass': 'pwd'}, true);
});

<!-- wait until a form tag disappears -->
casper.waitWhileSelector('form', function(){
    this.echo('selector is no more!');
});

casper.then(function(){
    this.echo(this.getTitle());
});
于 2015-07-30T06:15:08.647 回答
3

万一其他人发现了这个..我使用了这些答案的某种组合-我的登录表单也在 iframe 中,这增加了一些困难,但基本上我看到的问题(基于 cookie 的登录)是 casper 将进入下一步在服务器响应并设置 cookie 之前。我添加了一些 .wait() 回调以确保有足够的时间获取该 cookie。它可能不是万无一失的,但我还没有遇到问题

请注意,每次抓取都需要设置 cookie

casper.start(config.loginUrl, function() {

console.log("Checking login status @ " + config.loginUrl);

// set a wait condition to make sure the page is loaded (particularly iframe in my case) 
this.wait(5000,function(){

    // switch to iframe (won't be necessary for most)
    this.page.switchToChildFrame('login'); 

    // fill out the form 
    this.fillSelectors("form[name='loginForm']",{
        'input#txtUsername' : config.username,
        'input#txtPassword' : config.password
    });

    // click the login button 
    console.log("Logging In...")
    this.click('input.button.login');

    // ** give my crappy dev server 5 seconds to respond
    this.wait(5000,function(){
    console.log('Starting to spider ' + dataObj.start)

    // do yo dance  
    spider(dataObj.start);
    });
于 2014-09-11T16:24:23.230 回答
1

嗯.. 跟随:

casper.then(function () {
    this.evaluate(function () {
        $('form#user-login').submit();
    });
});
于 2013-10-24T15:33:31.840 回答