我正在尝试创建一个脚本来登录我的Facebook帐户,但是当我运行我的脚本时,我收到的消息是没有启用cookie。 我的phantom.js脚本:
var webPage = require('webpage');
var page = webPage.create();
page.settings.userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1';
page.settings.javascriptEnabled = true;
page.settings.loadImages = false;
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;
page.open("http://facebook.com", function(status) {
if ( status === "success" ) {
page.evaluate(function() {
document.querySelector("input[name='email']").value = "x";
document.querySelector("input[name='pass']").value = "y";
document.querySelector("#login_form").submit();
console.log("Login submitted!");
});
window.setTimeout(function () {
page.render('colorwheel.png');
phantom.exit();
}, 5000);
}
});
我做错了什么?
答案 0 :(得分:0)
一切正常,测试一下。您的问题是,您没有从沙箱中捕获console.log
消息,要执行此操作,您需要使用page.onConsoleMessage
回调。
function click(sel){var event=document.createEvent('MouseEvents');event.initMouseEvent('click',1,1,window,1,0,0,0,0,0,0,0,0,0,null);document.querySelector(sel).dispatchEvent(event);}
var page = require('webpage').create({viewportSize:{width: 1600,height: 900},
settings:{userAgent:'Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0',
javascriptEnabled:'true',
loadImages:'false'
}});
// https://github.com/dillbyrne/random-agent-spoofer/blob/master/data/json/useragents.json
page.onConsoleMessage = function (msg, line, source) {
console.log('console> ' + msg);
};
page.open("http://facebook.com", function(status) {
if ( status === "success" ) {
page.evaluate(function(click) {
document.querySelector("input[name='email']").value = "<login>";
document.querySelector("input[name='pass']").value = "<pass>";
document.querySelector("#login_form").submit();
console.log("Login submitted!");
// setTimeout(function(){ scrollTo(0,document.body.scrollHeight) },2500);//Scrolling to bottom.
//click("a[someAttr=someVal]")
},click);
setTimeout(function () {
page.render('test.png');
phantom.exit();
}, 5000);
}
});