似乎有三种方法可以做到这一点:
页面创建时
CasperJS 通过使用解决了这个问题page.onPageCreated http://phantomjs.org/api/webpage/handler/on-page-created.html。所以当window.open
在页面中调用,创建一个新页面并page.onPageCreated
由新创建的页面触发。
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
page.onPageCreated = function(newPage){
newPage.onLoadFinished = function(){
console.log(newPage.url);
phantom.exit();
};
};
page.evaluate(function(url){
window.open(url+"?something=other", "_blank");
}, address);
}
})
pages
PhantomJS'page
has a pages http://phantomjs.org/api/webpage/property/pages.html处理子页面的属性。所以,当你open
一个新的页面/标签,一个新的webpage
为该页面创建对象。您需要尝试添加一个onLoadFinished
在页面触发之前监听页面的事件(没有承诺)。这可能很困难,当window.open
从页面上下文中以未知的延迟调用。
这可以通过使用类似的东西来解决waitFor https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js等待新页面出现并在加载页面之前附加事件处理程序。这是经过小调整的完整代码。重试间隔从 250ms 减少到 50ms。
var page = require('webpage').create();
var address = "http://example.com/";
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
}
}
}, 50); //< repeat check every 50ms
};
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
console.log("p:", page.ownsPages, typeof page.pages, page.pages.length, page.pages);
waitFor(function test(){
return page.pages && page.pages[0];
}, function ready(){
page.pages[0].onLoadFinished = function(){
console.log("p:", page.ownsPages, typeof page.pages, page.pages.length, page.pages);
console.log("inner:", page.pages[0].url);
phantom.exit();
};
});
page.evaluate(function(url){
window.open(url+"?something=other", "_blank");
}, address);
}
})
代理解决方案
The window.open
函数可以被代理,并且在页面上下文中打开页面后,可以在幻像上下文中注册事件处理程序,并通过以下方式发出信号window.callPhantom
并陷入onCallback http://phantomjs.org/api/webpage/handler/on-callback.html.
page.onInitialized = function(){
page.evaluate(function(){
var _oldOpen = window.open;
window.open = function(url, type){
_oldOpen.call(window, url, type);
window.callPhantom({type: "open"});
};
});
};
page.onCallback = function(data){
// a little delay might be necessary
if (data.type === "open" && page.pages.length > 0) {
var newPage = page.pages[page.pages.length-1];
newPage.onLoadFinished = function(){
console.log(newPage.url);
phantom.exit();
};
}
};
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
page.evaluate(function(url){
window.open(url+"?something=other", "_blank");
}, address);
}
})