如何关注CasperJS中的所有链接? [英] How to follow all links in CasperJS?
问题描述
我在点击DOM中所有基于JavaScript的链接并保存
输出时遇到问题。链接的格式为
I'm having trouble clicking all JavaScript based links in a DOM and saving the output. The links have the form
<a id="html" href="javascript:void(0);" onclick="goToHtml();">HTML</a>
以下代码效果很好:
var casper = require('casper').create();
var fs = require('fs');
var firstUrl = 'http://www.testurl.com/test.html';
var css_selector = '#jan_html';
casper.start(firstUrl);
casper.thenClick(css_selector, function(){
console.log("whoop");
});
casper.waitFor(function check() {
return this.getCurrentUrl() != firstUrl;
}, function then() {
console.log(this.getCurrentUrl());
var file_title = this.getTitle().split(' ').join('_') + '.html';
fs.write(file_title, this.getPageContent());
});
casper.run();
但是,如何使用a的选择器,点击全部
可用链接和保存内容?我不知道如何让clickWhileSelector从选择器中删除节点,如下所示:单击与选择器匹配的所有链接
However, how can I get this to work with a selector of "a", clicking all available links and saving content? I'm not sure how to get the clickWhileSelector to remove nodes from the selector as is done here: Click on all links matching a selector
推荐答案
我有这个脚本,首先会获得所有链接一个页面然后将'href'属性保存到一个数组,然后迭代这个数组,然后逐个打开每个链接并回显网址:
I have this script that first will get all links from a page then save 'href' attributes to an array, then will iterate over this array and then open each link one by one and echo the url :
var casper = require('casper').create({
logLevel:"verbose",
debug:true
});
var links;
casper.start('http://localhost:8000');
casper.then(function getLinks(){
links = this.evaluate(function(){
var links = document.getElementsByTagName('a');
links = Array.prototype.map.call(links,function(link){
return link.getAttribute('href');
});
return links;
});
});
casper.then(function(){
this.each(links,function(self,link){
self.thenOpen(link,function(a){
this.echo(this.getCurrentUrl());
});
});
});
casper.run(function(){
this.exit();
});
这篇关于如何关注CasperJS中的所有链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!