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
1 Answer
Reset to default 6You 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
版权声明:本文标题:javascript - PhantomJS Login and Page Redirect - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742021137a2414696.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论