Skip to content Skip to sidebar Skip to footer

Injecting Javascript Code Into An On Click Event With Javascript And Casper.js

I've just started using casperjs after trying to use python (selenium / requests and mechanise) to scrape a page only after some javascript loaded some dynamic content on the page.

Solution 1:

I used a simple CSS selector for this task. You can change the onclick attribute with string operations. In this case I replace "100" with num. I also added a clickIt argument to click the changed link.

casper.changeItemsPerPageLink = function(num, clickIt){
    casper.evaluate(function(num, clickIt){
        num = ""+num;
        var a = document.querySelector('a[title="Show 100 items per page"]');
        a.innerHTML = num;
        a.title = a.title.replace("100", num);
        a.setAttribute("onclick", a.getAttribute("onclick").replace("100", num));
        if (clickIt) {
            a.click();
        }
    }, num, clickIt);
};

See my test code gist.

Post a Comment for "Injecting Javascript Code Into An On Click Event With Javascript And Casper.js"