I'm opening any page in the Amazon.com domain (for example "http://www.amazon.com") and then attempting to inject JQuery like this:
var injected = page.injectJs('jquery-1.7.1.js');
console.log("jquery was injected successfully: " + injected);
This code will print true
to the console. However, trying to access $(document)
from within page.evaluate()
like this:
page.onLoadFinished = function (status) {
var results = page.evaluate(function() {
$(document);
});
phantom.exit();
};
Will print TypeError: 'undefined' is not a function
to the console.
This code works with most other domains I've tried with. I noticed that Amazon seems to have its own version of JQuery that it loads and I wonder if it might be conflicting with the JQuery version I'm loading somehow. Any ideas?
You just need to put jQuery in noConflict mode with an alternate variable name like so:
var results = page.evaluate(function () {
$jq = window.jQuery;
$jq.noConflict();
console.log("Title: " + $jq('title').text());
});
Worked for me! Hope it works for you! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With