![](/img/trans.png)
[英]How to use PhantomJS's webpage.open callback in function return?
[英]PhantomJS: how to execute function on webpage
我需要執行簡單的腳本來點擊網站上的按鈕。 經過研究和閱讀文檔后,我編寫了以下腳本:
page.open('http://www.somewebsite.com/', function (status) {
if (status !== "success") {
console.log("Unable to access network");
} else {
console.log("Page loaded");
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate (function() {
function bump_trades() {
var xpr = document.evaluate(".//li/a[@class='trade_bump']/div[@class='icon_bump']", document, null, XPathResult.ANY_TYPE, null);
var anchor;
while (anchor = xpr.iterateNext()) {
anchor = anchor.parentNode;
if (anchor.getAttribute('data-tradeid'))
break;
}
if (anchor && anchor.getAttribute('data-tradeid')) {
setTimeout(function(){
anchor.click();
setTimeout(bump_trades, 500);
}, 500);
} else {
}
};
bump_trades();
});
console.log('Exit');
phantom.exit();
});
};
});
在瀏覽器控制台中,腳本本身(從var xpr ...開始)工作正常,沒有問題。 但是,在PhantomJS中,它什么也不做。 我有控制台消息,指出頁面已加載,但腳本未執行。 我是Java語言的新手,請幫助我找出問題所在。
您正在設置超時,但是要在實際執行之前退出。 在超時中移動phantom.exit()調用。
您可以添加一個變量,該變量在頁面上下文中的長期運行函數應終止時設置為true。 然后,您可以使用waitFor
構造在幻像上下文中等待退出。 所以改變
page.evaluate (function() {
function bump_trades() {
// your code
};
bump_trades();
});
console.log('Exit');
phantom.exit();
至
page.evaluate (function() {
function bump_trades() {
var xpr = document.evaluate(".//li/a[@class='trade_bump']/div[@class='icon_bump']", document, null, XPathResult.ANY_TYPE, null);
var anchor;
while (anchor = xpr.iterateNext()) {
anchor = anchor.parentNode;
if (anchor.getAttribute('data-tradeid'))
break;
}
if (anchor && anchor.getAttribute('data-tradeid')) {
setTimeout(function(){
anchor.click();
setTimeout(bump_trades, 500);
}, 500);
} else {
window.bumpTradesCompleted = true;
}
};
window.bumpTradesCompleted = false;
bump_trades();
});
waitFor(function testFx(){
return page.evaluate(function(){
return window.bumpTradesCompleted;
});
}, function onReady(){
phantom.exit();
}, 1000000); // wait a long time
waitFor
函數如下所示:
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log("'waitFor()' timeout");
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}, 250); //< repeat check every 250ms
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.