How to print text of all loc tags using casperjs

336 views Asked by At

I need to extract loc tags text of entire page and get the count of all extracted text .Iterate in a loop for following command casper.open('http://www.raybiztech.com/.....') for different iterations. Can any one help for the above scenario

<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
        <url>
            <loc>http://raybiztech.com/blog</loc>
            <lastmod>2014-02-24</lastmod>
        </url><url>
            <loc>http://raybiztech.com/blog/application-development-maintenance</loc>
            <lastmod>2014-02-14</lastmod>
        </url><url>
            <loc>http://raybiztech.com/blog/application-development-maintenance/analysis-services-testing-with-nick</loc>
            <lastmod>2014-02-14</lastmod>
        </url><url>
            <loc>http://raybiztech.com/blog/application-development-maintenance/bi-solution-join-on-premises-to-windows-azure</loc>
            <lastmod>2014-02-14</lastmod>
        </url><url>
            <loc>http://raybiztech.com/blog/application-development-maintenance/data-profiling-im-data-source-view</loc>
            <lastmod>2014-02-14</lastmod>
        </url><url>
            <loc>http://raybiztech.com/blog/application-development-maintenance/data-programmability-tools-with-craig-lee</loc>
            <lastmod>2014-02-14</lastmod>
 .....

I updated the code as below.I want extract text in particular "loc" and iterate in casper.thenopen("http://raybiztech.com/blog");

var casper = new require('casper').Casper({
    verbose: true,          // log messages will be printed out to the console
    logLevel: "debug",              // "Debug" level messages will be logged        
    safeLogs: false,


});
    var fs = require('fs');
        var fname = new Date().getTime() + '.xml';
    var save = fs.pathJoin(fs.workingDirectory, 'raybiztech', fname);

casper.test.begin('Payment Gateway',function(test) {

/*
 * Start the browser with url http://lcdev.bluemodus.com/
 */
casper.start('http://www.raybiztech.com/sitemap.xml',function(){
    this.capture('/home/gopi/output/learningcounts/cmsHome.jpg');
    this.echo(this.getCurrentUrl());
    fs.write(save, this.getPageContent() + '\n', 'w');
    require('utils').dump(this.getElementsInfo('loc'));


});
casper.viewport(1366,768);
casper.run(function() {
    test.done();
    casper.test.renderResults(true, 0, '/home/gopi/workspace/learningcounts/raybiztech.xml');
});
});
1

There are 1 answers

2
Artjom B. On

The function casper.getElementsInfo returns an array with every item containing the text property. You can then iterate over the array with map to transform the objects to the text property string.