Skip to content Skip to sidebar Skip to footer

Can't Find Variable - Phantomjs

I post here after many hours of fruitless searching. PhantomJS does not allow me to use a variable as in the code below, with the error message when running my script 'Can not find

Solution 1:

There is an important piece of information in the Quick Start tutorial (in its Code Evaluation section):

To evaluate JavaScript or CoffeeScript code in the context of the web page, use evaluate() function. The execution is "sandboxed", there is no way for the code to access any JavaScript objects and variables outside its own page context. An object can be returned from evaluate(), however it is limited to simple objects and can't contain functions or closures.

The problem with your code is thus twofold:

  1. Variable elem is initialized outside the web page context, it's not reachable from the second evaluate.
  2. You return a non-simple object, i.e. a DOM element.

This is an easy problem to solve, mainly by properly designing the code to fit the actual "jailed" execution model. Please carefully read all relevant documentation and explore tons of included examples.

Post a Comment for "Can't Find Variable - Phantomjs"