Skip to content Skip to sidebar Skip to footer

Casperjs Inject Javascript Via This.evaluate

Today I tried to inject some javascript logic into remote page using CasperJS with PhantomJS together. Well, I'm quite surprised because of that: casper.then(function() { conso

Solution 1:

You cannot do what you are thinking. myMethod is private to the function that is passed to this.evaluate. It doesn't make sense that you would expect a private function to be available in another function, just because it was passed to the same method.

In your example, you could try

casper.then(function() {
    functionmyMethod() {
        return'Any thing?!';
    }
    console.log(this.evaluate(function() {
        returnmyMethod();
    }));
    console.log(this.evaluate(function() {
        returnmyMethod();
    }));
});

But that is probably not what you are looking for? Or is it?

Or are you trying to attach code to the page itself? Maybe the following?

casper.then(function() {
    console.log(this.evaluate(function() {
        // Just creating a variable won't attach it to the window, it will// be local to the function. However, you can attach it to the window// object, which is in the scope chainwindow.myMethod = function () {return'anything'};
    }));
    console.log(this.evaluate(function() {
        returnmyMethod(); // or window.myMethod();
    }));
});

Post a Comment for "Casperjs Inject Javascript Via This.evaluate"