Open willin opened 7 years ago
PhantomJS does not support ES6 syntax. In the examples above, you use () =>
when sending functions to page.evaluate
. Functions sent to page.evaluate
must conform to ES5 syntax. Try replacing () =>
with function()
page.evaluate(function () {
return [1, 2, 3].map(x => x + 1);
})
yes, i tried this get null
but in browser:
page.evaluate(function () {
return [].slice.call(document.querySelectorAll('tr')).slice(5, document.querySelectorAll('tr').length - 3);
}, (err4, result) => {
console.log(result);
browser.exit();
});
opened site? success
[]
page.evaluate(function () {
return [1, 2, 3].map(x => x + 1);
})
This will not work in PhantomJS as you are using ES6 syntax. Arrow functions () => {}
were introduced in ES6, which PhantomJS does not support.
The correct syntax would be:
page.evaluate(function () {
return [1, 2, 3].map(function(x) { return x + 1});
});
The other example of yours, with tr
elements will most likely not work either, since you're returning a list of DOM Elements that cannot be turned into strings.
Keep this in mind when using page.evalulate
:
The function you are sending into the function will be turned into a string and then executed with eval
or new Function
. This means that it will not have access to anything outside of the scope of itself (and the browser scope, so objects like document
and window
are available). The values it takes as arguments as well as return values has to be JSONinifable.
if i use:
can print html result.
using basic example: