user1846022
user1846022

Reputation: 41

How to get get URL of new page using casperJS

I am using casperJS to get links when clicking a button. The links are returned with window.open in javaScript.

The code I have written logs all the pages after clicking button, but phantom is not exiting in terminal window. Also some pages only show about:blank, especially the last ones.

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();
    });

}

Upvotes: 2

Views: 5994

Answers (2)

Grant Miller
Grant Miller

Reputation: 28989

You can get the URL of the current page in CasperJS using one of the following methods:

Upvotes: 2

Christian Michael
Christian Michael

Reputation: 2316

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();
        });

});

Upvotes: 2

Related Questions