JCoder23
JCoder23

Reputation: 551

CasperJs: Clicking links in a loop to open modal popup

I have a page that load list of items with a 'Details' link. Clicking this link opens a modal popup which contains the data i need.

I want to loop through each of these 'Details' links, clicking it to open the modal popup, read the data, close it and continue to the next.

How would i go about doing this ? Im not sure how to trigger a remote click in a loop to open the modal, followed by a '.waitForSelector' to read the info in the modal.

So far i have:

casper.start(url);

// How do i loop these two steps ??
casper.then(function() {
    this.evaluate(function() {
        var detailsBtn = Zepto('.details').first();
        detailsBtn.trigger('click');
    });
});

casper.waitForSelector('#popup-window', function() {
    // read data from popup
});

This works fine for one link :)

Any help would be greatly appreciated !

Thanks

Upvotes: 1

Views: 1703

Answers (1)

JCoder23
JCoder23

Reputation: 551

Ok i figure it out with some help on the CasperJS group

Here is the solution for anyone trying something similar:

casper.start(url);

var i = 1;
casper.then(function loadResults(){
    var linkCount = this.getElementsInfo('.link').length;
    console.log('Found ' + linkCount + ' links.');

    this.repeat(linkCount, function() {

        try {

            this.click('.link'); // opens modal popup

            this.waitUntilVisible('#modal', function() {

                // do work

            });

            this.click('#close'); // close modal popup

        } catch(err) {
            console.log(err);
        } finally {
            i++;
        }

    });

});


casper.run();

Upvotes: 2

Related Questions