admin管理员组

文章数量:1317131

I am trying to crawl a website which kind of have a login page seperated and jumping homepage after login. Here is my code, but I am not acplishment jumping homepage:

var page = require('webpage').create() ;
var login = '' ;
var home = '' ;

page.open(login, function (status) {
    if (status !== 'success') {
        console.log('fail!');
    } else {
        page.evaluate(function(){
            function timer (f,n) {
                var i = 0 ;
                var t = setInterval(function(){
                    if (n < i) {
                        clearInterval(t) ;
                        f() ;
                    }
                    i++ ;
                },50) ;
            }
            $("input[name=email]").val("user") ;
            $("input[name=password]").val("pass") ;
            $("input[type=submit]").click() ;
            timer(function(){
                document.location.href = home ;
                timer(function(){
                    $('body').css('border','1px solid red') ;
                },100) ;
            },100) ;
        }) ;
        page.render('page.png') ;
    }
    console.log('finished!') ;
    phantom.exit() ;
});

I am trying to crawl a website which kind of have a login page seperated and jumping homepage after login. Here is my code, but I am not acplishment jumping homepage:

var page = require('webpage').create() ;
var login = 'https://webstie./login' ;
var home = 'https://website./home' ;

page.open(login, function (status) {
    if (status !== 'success') {
        console.log('fail!');
    } else {
        page.evaluate(function(){
            function timer (f,n) {
                var i = 0 ;
                var t = setInterval(function(){
                    if (n < i) {
                        clearInterval(t) ;
                        f() ;
                    }
                    i++ ;
                },50) ;
            }
            $("input[name=email]").val("user") ;
            $("input[name=password]").val("pass") ;
            $("input[type=submit]").click() ;
            timer(function(){
                document.location.href = home ;
                timer(function(){
                    $('body').css('border','1px solid red') ;
                },100) ;
            },100) ;
        }) ;
        page.render('page.png') ;
    }
    console.log('finished!') ;
    phantom.exit() ;
});
Share Improve this question asked Oct 18, 2015 at 10:10 DigerkamDigerkam 1,9194 gold badges24 silver badges41 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You forgot to wait for asynchronous processing. Your timer() function is asynchronous, because setTimeout() is asynchronous. That is why your page.render() call happens actually before the timer() has run. The same goes for phantom.exit().

But you don't want to use document.location.href = home, because then you need to listen to the page open event. You can do this in an integrated fashion with another page.open().

Try:

page.open(login, function (status) {
    if (status !== 'success') {
        console.log('fail!');
        phantom.exit(1);
    } else {
        page.evaluate(function(){
            $("input[name=email]").val("user") ;
            $("input[name=password]").val("pass") ;
            $("input[type=submit]").click() ;
        });
        setTimeout(function(){
            page.open(home, function(status){
                if (status !== "success") {
                    console.log('fail2');
                    phantom.exit(1);
                    return;
                }
                page.evaluate(function(){
                    $('body').css('border','1px solid red') ;
                });
                page.render('page.png');
                console.log('finished!');
                phantom.exit();
            });
        }, 500);
    }
});

Use waitFor() for more robust waiting for a specific condition or use the page.onCallback and window.callPhantom() pair.

本文标签: javascriptPhantomJS Login and Page RedirectStack Overflow