Back to Top

Casperjs : How to Save All href and title in an Array

Updated 20 January 2017

For the installation and basic CasperJs knowledge you can check here. Now we’ll proceed with the testing process.

To get the all href and title of a selector we have used arrays to save them. you can check the following process.

1) To get the all href and title of a selector we have created two variables with blank arrays.

var links = [];
var title = [];

2) Then we have created two functions getLinks() and getTitle().

function getLinks() {
    var links = document.querySelectorAll('your-selector');
    return Array.prototype.map.call(links, function(e) {
        return e.getAttribute('href');
    });
};
function getTitle() {
    var title = document.querySelectorAll('your-selector');
    return Array.prototype.map.call(title, function(a) {
        return a.getAttribute('title');
    });
};

3) Then we have started the casper and used cli.get(0) for giving the url of the webpage from terminal and we have used getCurrentUrl() to get the current url.

Start your headless eCommerce
now.
Find out More

4) Then we have used evaluate() to save values in variables ‘links’ and ‘title’.

casper.then(function() {
    links = this.evaluate(getLinks);
});
casper.then(function() {
    title = this.evaluate(getTitle);
});

5) Now we have fetched all the links and titles from the array.

Here, you can check complete code –

/**
* Webkul Software.
*
* @category Webkul
* @package Webkul_CasperJS
* @author Webkul
* @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/

var links = [];
var title = [];
var casper = require("casper").create();

function getLinks() {
    var links = document.querySelectorAll('your-selector');
    return Array.prototype.map.call(links, function(e) {
        return e.getAttribute('href');
    });
};
function getTitle() {
    var title = document.querySelectorAll('your-selector');
    return Array.prototype.map.call(title, function(a) {
        return a.getAttribute('title');
    });
};
casper.start(casper.cli.get(0), function() {
     this.echo('Current location is =' + this.getCurrentUrl(), 'COMMENT');

});

// save values in arrays
casper.then(function() {
    links = this.evaluate(getLinks);
});
casper.then(function() {
    title = this.evaluate(getTitle);
});

casper.run(function() {
    // echo results in a desired pattern
    this.echo(links.length + ' links found:', 'GREEN_BAR');
    this.echo(' - ' + links.join('\n - '));
    this.echo(title.length + ' title found:', 'GREEN_BAR');
    this.echo(' - ' + title.join('\n - ')).exit();
});

Now you have to run the command and you will get your result-
casperjs file_name.js http://your-page-url/

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Jesal
    • Neelesh Singh (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home