I am using node-phantom to post items to a cart on a web site. The problem is that when I use page.open to navigate to the shopping cart page (after already having added an item to the cart), I get an html response saying that I need to enable javascript in my browser in order to view the shopping cart page. I've checked the settings.javascriptEnabled setting and found it to be set to 'true'. At this point I am confused, why does the page think that phantomjs does not have javascript enabled?
Here is my code:
var phantom = require('node-phantom');
phantom.create(function (err, ph) {
ph.createPage(function (err, page) {
page.get('settings', function(err, oldSettings) {
console.log('\r\n oldSettings: ' + JSON.stringify(oldSettings));
page.open('http://www.somesite.com/shoppingcart/default.cfm', function (err, status) {
page.injectJs(jqueryPath, function (err) {
setTimeout(function() {
page.evaluate(function (injectedSku) {
var localErr;
var skuInCart;
var checkoutLnkMsg;
var pageHTML;
try {
pageHTML = $("html").html();
// Get 'SKUs' input element.
skuInCart = $('input[name="SKUs"]').val();
if (injectedSku === skuInCart) {
var checkoutLnk = $('#cartAction_bottom a[alt="Checkout"');
checkoutLnk.on("click", function() {
checkoutLnkMsg = '"' + checkoutLnk.href + '" link has been clicked';
});
checkoutLnk.click();
} else {
throw new Error('Product not in cart');
}
} catch (e) {
localErr = e;
}
return {
pageHTML: pageHTML,
err: localErr,
skuInCart: skuInCart,
checkoutLnkMsg: checkoutLnkMsg,
injectedSku: injectedSku
};
}, function (err, result) {
if (result.err) {
callback(err);
//return ph.exit();
}
fs.writeFileSync("./html_log.txt", result.pageHTML);
console.log('\r\n checkout - page.evaluate - injectedSku: ' + result.injectedSku);
console.log('\r\n checkout - page.evaluate - result.skuInCart: ' + JSON.stringify(result.skuInCart));
console.log('\r\n checkout - page.evaluate - result.checkoutLnkMsg: ' + result.checkoutLnkMsg);
callback(null);
//return ph.exit();
}, sku);
}, 1250);
});
});
});
});
});
Replace
page.injectJs()
with
page.includeJs()
UPDATE
var phantom = require('node-phantom');
phantom.create(function (err, ph) {
ph.createPage(function (err, page) {
page.get('settings', function(err, oldSettings) {
//console.log('\r\n oldSettings: ' + JSON.stringify(oldSettings));
page.open('http://www.footlocker.com/shoppingcart/default.cfm?', function (err, status) {
console.log(status);
var sku = 234; // assign sku id here
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js', function (err) {
setTimeout(function() {
page.evaluate(function (injectedSku) {
var localErr;
var skuInCart;
var checkoutLnkMsg;
var pageHTML;
try {
pageHTML = $("html").html();
// Get 'SKUs' input element.
skuInCart = $('input[name="SKUs"]').val();
if (injectedSku === skuInCart) {
var checkoutLnk = $('#cartAction_bottom a[alt="Checkout"');
checkoutLnk.on("click", function() {
checkoutLnkMsg = '"' + checkoutLnk.href + '" link has been clicked';
});
checkoutLnk.click();
} else {
throw new Error('Product not in cart');
}
} catch (e) {
localErr = e;
}
return {
pageHTML: pageHTML,
err: localErr,
skuInCart: skuInCart,
checkoutLnkMsg: checkoutLnkMsg,
injectedSku: injectedSku
};
}, function (err, result) {
if (result.err) {
// callback(err);
//return ph.exit();
}
// fs.writeFileSync("./html_log.txt", result.pageHTML);
console.log('\r\n checkout - page.evaluate - injectedSku: ' + result.injectedSku);
console.log('\r\n checkout - page.evaluate - result.skuInCart: ' + JSON.stringify(result.skuInCart));
console.log('\r\n checkout - page.evaluate - result.checkoutLnkMsg: ' + result.checkoutLnkMsg);
// callback(null);
ph.exit();
}, sku);
}, 1250);
});
});
});
})
});
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