var steps=[];
var testindex = 0;
var loadInProgress = false;//This is set to true when a page is still loading
var webPage = require('webpage');
var page = webPage.create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36';
page.settings.javascriptEnabled = true;
page.settings.loadImages = false;//Script is much faster with this field set to false
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;
console.log('All settings loaded, start with execution');
page.onConsoleMessage = function(msg) {
console.log(msg);
};
steps = [
function(){
console.log('Step 1 - Open Fb home page');
page.open("https://www.facebook.com/login.php", function(status){
});
},
function(){
console.log('Step 3 - Populate and submit the login form');
page.evaluate(function(){
document.getElementById("email").value="[email protected]";
document.getElementById("pass").value="xxx";
document.getElementById("login_form").submit();
});
},
function() {
page.render('homepage.png');
},
function(){
page.open("https://www.facebook.com/settings", function(status){
});
},
function() {
page.render('settings.png');
},
];
interval = setInterval(executeRequestsStepByStep,50);
function executeRequestsStepByStep(){
if (loadInProgress == false && typeof steps[testindex] == "function") {
//console.log("step " + (testindex + 1));
steps[testindex]();
testindex++;
}
if (typeof steps[testindex] != "function") {
console.log("test complete!");
phantom.exit();
}
}
page.onLoadStarted = function() {
loadInProgress = true;
console.log('Loading started');
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log('Loading finished');
};
page.onConsoleMessage = function(msg) {
console.log(msg);
};
如何注入 「https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js」,並使用如何在Phantomjs中注入jquery(js)?
$('#email').attr("value", "[email protected]");
$('#pass').attr("value", "xxxx");
$('#login_form').submit();
我嘗試所有方法,但劇本着工作。
我沒有看到你試圖在代碼中使用jQuery的地方。請[編輯]您的問題以顯示實際的代碼。你怎麼知道它不起作用?你是否等了一段時間?你有截圖嗎? –