我有一個工作腳本,看起來有點...登錄並加載與PhantomJS網站後,沒有硬編碼的時間
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.open("http://www.any_website.com", function(status) {
if (status == "success") {
page.evaluate(function() {
document.querySelector("input[name='MAIL_ADDRESS']").value = "[email protected]";
document.querySelector("input[name='PASSWORD']").value = "the_real_password";
document.getElementsByName("LOGIN_FORM_SUBMIT")[0].click();
console.log("Login submitted!");
});
window.setTimeout(function() {
var ua = page.evaluate(function() {
return document.getElementById('ContentMain').innerHTML;
});
console.log(ua);
phantom.exit();
}, 20000);
}
});
至於好。
但正如您所看到的,我在點擊登錄按鈕後20秒內實施了修復超時。我想擺脫這一點,我希望腳本在登錄完成後立即關閉。我現在玩了好幾個月,但我沒有找到一個沒有時間限制的解決方案,這將更加優雅,高效和強大。
有人可以幫助改編的代碼嗎?
感謝
PS:關於JavaScript + phantomjs的功能更多相關信息,歡迎選購。我並不真正知道我在這裏做什麼,我不知道第二頁是否有意義。
PPS:是否有延遲功能,等待網站完全加載?
編輯1:
謝謝您的意見。我可以精確地「完全加載」,以便在數據中出現一個定義的字符串。我試着用setInterval循環並在html數據中尋找特定的字符串。
這個新代碼不工作,因爲腳本在步驟1後掛起。我想當我讀出page.content值時,整個phantomjs處理停止,我不會得到page.content提前它不會得到隨時登錄後的最新數據。
該計劃只是爲了輪詢html數據,只要我找到一個特定的字符串,我知道將在網站加載時出現。
當我將間隔增加到5000或更高時,可能是因爲在最終數據出現後調用了page.content,該腳本可以工作?! (不知道,但這是我的解釋)
任何想法如何輪詢html數據沒有打破/停止網站下載/處理?
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) {
'use strict';
if (typeof start !== 'number') {
start = 0;
}
if (start + search.length > this.length) {
return false;
} else {
return this.indexOf(search, start) !== -1;
}
};
}
var page = require('webpage').create(), testindex = 0, loadInProgress = false, delayedLoad = false;
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
var steps = [
function() {
//Load Login Page
page.open("http://www.any_website.com");
},
function() {
//Enter Credentials and login
page.evaluate(function() {
document.querySelector("input[name='MAIL_ADDRESS']").value = "real_name";
document.querySelector("input[name='PASSWORD']").value = "real_password";
document.getElementsByName("LOGIN_FORM_SUBMIT")[0].click();
});
},
function() {
// Output content of page to stdout after form has been submitted
page.render('out.png');
page.evaluate(function() {
console.log(document.getElementById('ContentMain').innerHTML);
});
}
];
// this is for signalizing phantomjs when all the data has finished loading
var stepstop = [ "", "Stop Text at the End of the needed Data", ""];
interval = setInterval(function() {
if (!loadInProgress && typeof steps[testindex] == "function") {
if (delayedLoad == false) {
console.log("step " + testindex);
steps[testindex]();
}
if (stepstop[testindex] != "") {
var tempHTML = page.content;
// console.log("b " + tempHTML.length);
console.log("c " + stepstop[testindex]);
// console.log("d " + tempHTML);
console.log("e " + tempHTML.includes(stepstop[testindex]));
if (tempHTML.includes(stepstop[testindex]) != false) {
console.log("step " + testindex + ": HTML stop found");
delayedLoad = false;
testindex++;
} else {
console.log("step " + testindex + ": HTML stop not found");
delayedLoad = true;
}
} else {
console.log("step " + testindex + ": no HTML stop search needed");
testindex++;
}
}
if (typeof steps[testindex] != "function") {
console.log("shutdown phantom");
phantom.exit();
}
}, 100);
看看這裏:http://stackoverflow.com/questions/9246438/how-to-submit-a-form-using-phantomjs - 這解決您的問題,如果我沒有記錯。 – Tomalak
[phantomjs不等待「完整」頁面加載]可能的重複(http://stackoverflow.com/questions/11340038/phantomjs-not-waiting-for-full-page-load) –
這兩種方法是最好的: [檢查未完成的網絡活動](http://stackoverflow.com/a/21401636/1816580)和[檢查所有請求已完成](http://stackoverflow.com/a/14748934/1816580)。當然,你總是可以重新使用'waitFor'來等待一個特定的選擇器,這意味着一個頁面被完全加載。 –