本文介绍了如何使用casperJS获取新页面的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
单击按钮时,我正在使用casperJS来获取链接.链接通过javaScript中的window.open返回.
I am using casperJS to get links when clicking a button. The links are returned with window.open in javaScript.
单击按钮后,我编写的代码记录了所有页面,但幻影未在终端窗口中退出.另外,有些页面只显示about:blank,尤其是最后一页.
var casper = require('casper').create();
var page = require('webpage').create();
var address = 'http://www.example.com';
page.open(address, function() {
page.onPageCreated = function(newPage) {
newPage.onClosing = function(closingPage) {
console.log('A child page is closing: ' + closingPage.url);
/* if i set phantom.exit() it will only log the first page url.
Problem: I need all page urls. */
}
}
page.evaluate(function() {
$(".button").click();
});
}
推荐答案
方法"getCurrentUrl()"将返回当前网址.这是一个简单的(不是很有意义的)示例:
The method "getCurrentUrl()" will return the current url. Here is an easy (not really meaningful) example:
casper.test.begin('My Test', 1 , function suite(test) {
casper.start().viewport(1600,1000).thenOpen("https://blabla.de", function() {
var mylink = this.getElementInfo("#linkid").text;
this.clickLabel(mylink);
});
casper.waitForSelector("#page2", function() {
this.echo(this.getCurrentUrl());
});
casper.run(function() {
test.done();
});
});
这篇关于如何使用casperJS获取新页面的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!