casperjs: Howto get links that contain text? -
i try that: not work
function getlinks(containtext) { return casper.evaluate(function(containtext) { var links = document.queryselectorall('a'); return array.prototype.map.call(links, function (e) { var href = e.getattribute('href'); console.log(href); if (href.indexof(containtext) !== -1) { return href; } }); }) } links = getlinks('intermediary'); require('utils').dump(links );
also console.log not seem work: can use inside evaluate() ?
var casper = require('casper').create(); function getlinks(containtext) { var links = document.queryselectorall('a'); return array.prototype.map.call(links, function(e) { return e.getattribute('href'); }).filter(function(e) { return e.indexof(containtext) !== -1; }); } casper.start('file:///tmp/test.html', function() { var links = this.evaluate(getlinks, 'intermediary'); require('utils').dump(links); }); casper.run();
you correct console.log
won't work within evaluate()
since running inside context of web page's dom: http://docs.casperjs.org/en/latest/modules/casper.html#casper-evaluate
here's sample /tmp/test.html
show filtering works:
<html> <head> <title>test</title> </head> <body> <p>here example pages.</p> <p><a href="intermediary">a link</a></p> <p><a href="click">click</a></p> <p><a href="this contains string intermediary in it">other link</a></p> <p><a href="this not contain string">yet link</a></p> </body> </html>
and output:
[ "intermediary", "this contains string intermediary in it" ]
Comments
Post a Comment